Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to use EntraID auth and Identity on .NET 8

$
0
0

Im trying to get AspNet Identity and EntraID(AzureAD) to work together in my Blazor .net 8 project. In my Programs.cs i have this

builder.Services.AddAuthorization();builder.Services.AddAuthentication("MyCookie")    .AddCookie("MyCookie").AddMicrosoftAccount(options =>{    builder.Configuration.GetSection("Authentication:Microsoft").Bind(options);});builder.Services.AddIdentity<ApplicationUser, ApplicationRole>()    .AddEntityFrameworkStores<ApplicationDbContext>()    .AddRoleManager<RoleManager<ApplicationRole>>()    .AddDefaultTokenProviders();

and have this controller for external logins

        public static IEndpointRouteBuilder MapIdentityEndpoints(this IEndpointRouteBuilder endpoints)    {        ArgumentNullException.ThrowIfNull(endpoints);        var accountGroup = endpoints.MapGroup("/AccountExternal");        #region Microsoft Login        accountGroup.MapGet("/PerformMicrosoftLogin", () => TypedResults.LocalRedirect($"/"));        accountGroup.MapPost("/PerformMicrosoftLogin", async (            HttpContext context,            [FromForm] string returnUrl            ) =>        {            string provider = "Microsoft";            var properties = new AuthenticationProperties { RedirectUri = returnUrl };            await context.SignOutAsync();            return TypedResults.Challenge(properties, [provider]);        });        #endregion        return accountGroup;    }

If i comment this part

builder.Services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddRoleManager<RoleManager<ApplicationRole>>().AddDefaultTokenProviders();

I can auth via Microsoft. But if I uncomment it and try signing in via Microsoft, it takes me through all the verification steps but will not authorize me. Just sends me back to the login page.


Viewing all articles
Browse latest Browse all 4839

Trending Articles