Microsoft Identity and OpenIdConnection

Enrico Rossini 186 Reputation points
2021-02-04T15:04:35.61+00:00

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?
Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
661 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,141 questions
{count} vote