Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
manoj-choudhari-git / app.module.ts
Last active April 22, 2020 20:28
This is demonstration of how Azure AD authentication can be configured for Angular application
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { MsalModule, MsalInterceptor } from '@azure/msal-angular';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
export const protectedResourceMap: [string, string[]][] = [
['https://graph.microsoft.com/v1.0/me', ['user.read']]
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MsalGuard } from '@azure/msal-angular';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{
path: '',
canActivate: [
MsalGuard
import { Component, OnInit } from '@angular/core';
import { AuthService } from './auth.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'SecureApp';
@manoj-choudhari-git
manoj-choudhari-git / app.component.html
Created April 22, 2020 21:17
App Component Template
<router-outlet></router-outlet>
<button class="dropdown-item" (click)="logOut()">Logout</button>
@manoj-choudhari-git
manoj-choudhari-git / auth.service.ts
Last active April 22, 2020 21:19
Authentication Service
import { Injectable } from '@angular/core';
import { BroadcastService, MsalService } from '@azure/msal-angular';
import { Logger, CryptoUtils } from 'msal';
@Injectable()
export class AuthService {
isIframe = false;
loggedIn = false;
constructor(
@manoj-choudhari-git
manoj-choudhari-git / Startup.cs
Created April 29, 2020 19:18
Startup for Web API Secured by Azure AD
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
@manoj-choudhari-git
manoj-choudhari-git / appsettings.json5
Last active April 29, 2020 19:27
AppSettings.JSON for Web API secured by Azure AD
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
/*******************************************************
Tenant ID identifying Azure AD instance
Enter 'common', or 'organizations' or the Tenant Id
Obtained from the Azure portal. Select 'Endpoints'
from the 'App registrations' blade
*******************************************************/
@manoj-choudhari-git
manoj-choudhari-git / _logout.cshtml
Created May 1, 2020 18:18
Login and Logout Links
@if (User.Identity.IsAuthenticated)
{
<li class="nav-item">
<span class="navbar-text text-dark">Hello @User.Identity.Name!</span>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="MicrosoftIdentity"
asp-controller="Account" asp-action="SignOut">
Sign out
</a>
@manoj-choudhari-git
manoj-choudhari-git / Startup.cs
Last active May 1, 2020 18:25
Startup Configuration for MVC Web Application
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
@manoj-choudhari-git
manoj-choudhari-git / appsettings.json5
Created May 1, 2020 18:29
AppSettings.json for MVC Application
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "da41245a5-11b3-996c-00a8-4d99re19f292",
"ClientId": "5a886671-26ae-4844-84d8-19cb3e1cfbb5",
"CallbackPath": "/signin-oidc",
"SignedOutCallbackPath ": "/signout-callback-oidc"
},
"Logging": {
"LogLevel": {