I have an angular7 application with asp.net core 2.2 and an Identity server 4 for authentication and authorization. I am trying to do hybrid or implicit grant type to authenticate our angular application. Asp.net core is bootstraping our angular7 application using below code in startup.cs
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
I want user should redirect to Identity server for authentication and after authentication our angular application should open. For this I have added below code in startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:9001";
options.RequireHttpsMetadata = false;
options.ClientId = "AngularWebApp";
options.ClientSecret = "the_secret";
options.ResponseType = "code id_token";
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.Scope.Add("openApiScope");
options.Scope.Add("offline_access");
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
}
But this code is not redirecting automatically as I don't have any Controller and action method defined in asp.net core which uses [Authorize]
So what I tried to redirect automatically, I written code in AppModule class of Angular to check the user login status if not logged in then redirect the user to identity server using below code using oidc.client.js .
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
HttpClientModule,
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private _authService: AppAuthNService) {
from(this._authService._userManager.getUser().then((user) => {
if (user==null) {
this._authService.login();
}
}));
}
}
It is now redirecting to authorization server but I am not sure if it is good approach.
Can any one suggest us