Skip to content

Instantly share code, notes, and snippets.

@shammelburg
Last active August 30, 2019 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shammelburg/eb7feade36673669c45d6cd7f6cd46eb to your computer and use it in GitHub Desktop.
Save shammelburg/eb7feade36673669c45d6cd7f6cd46eb to your computer and use it in GitHub Desktop.
Angular WebAPI AzureAD snippets
export class AppComponent {
constructor(private adal: AdalService) {
this.adal.init(environment.adalConfig);
}
signOut(): void {
this.adal.logOut();
}
}
@NgModule({
declarations: [
AppComponent,
AuthCallbackComponent
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule
],
providers: [
AdalService,
{ provide: HTTP_INTERCEPTORS, useClass: AdalInterceptor, multi: true }
],
bootstrap: [AppComponent]
})
export class AppModule { }
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "your AD domain (either custom or .onmicrosoft.com)",
"TenantId": "Azure AD Directory ID",
"ClientId": "Registered App (angular-aad-api)"
}
}
export class AuthCallbackComponent implements OnInit {
constructor(
private router: Router,
private adal: AdalService,
private zone: NgZone) { }
ngOnInit() {
this.adal.handleWindowCallback();
setTimeout(() => {
this.zone.run(
() => this.router.navigate(['/'])
);
}, 200);
}
}
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private adal: AdalService) { }
canActivate(): boolean {
if (this.adal.userInfo.authenticated) {
return true;
}
this.adal.login();
return false;
}
}
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
}
export const environment = {
adalConfig: {
tenant: 'The Azure AD Directory ID',
clientId: 'The Registered Angular App Application ID',
redirectUri: "The successful login URL (http://localhost:4200/auth-callback)",
postLogoutRedirectUri: "The successful logged out URL (http://localhost:4200/logged-out",
endpoints: {
"The URL which will access this app": "The Registered Web API Application ID"
}
}
};
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins", builder =>
{
builder.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
app.UseCors("AllowAllOrigins");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment