Microsoft Identity and OpenIdConnection
![](https://techprofile.blob.core.windows.net/images/da03c55ce18f412eb346cb3f4c6f0bda.jpg)
Enrico Rossini
186
Reputation points
I'm trying to connect my web application with .NET5 and the new Microsoft.AspNetCore.Authentication.OpenIdConnect
. Apparently, my old code for OpenIdConnect
is valid but it doesn't work. This is what I'm adding to the ConfigureServices:
services.AddSession(options =>
{
options.Cookie.Name = ".puresourcecode.session";
options.IdleTimeout = TimeSpan.FromHours(12);
});
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.Cookie.Name = "puresourcecode.cookie";
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = idsrv.IdentityServerUrl;
options.ClientId = idsrv.ClientId;
options.ClientSecret = idsrv.ClientSecret;
#if DEBUG
options.RequireHttpsMetadata = false;
#else
options.RequireHttpsMetadata = true;
#endif
options.ResponseType = "code";
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("roles");
options.Scope.Add("offline_access");
options.ClaimActions.MapJsonKey("role", "role", "role");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.SignedOutRedirectUri = "/";
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = JwtClaimTypes.Name,
RoleClaimType = JwtClaimTypes.Role,
};
});
and this is what I add to the Configure:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Then, 2 questions:
- is it possible to connect a .NET5 (ASP.NET) project to IdentityServer4?
- is it possible use Microsoft Identity and
OpenIdConnect
in the same project?