首页 > 解决方案 > AzureAD not working with IdentityUser and IdentityRole

问题描述

I'm trying to get AzureAD authentication implemented in my app, which has role authentication. When I try to run the app, instead of automatically going to Microsoft login page (like it does in new project made apps), it goes to login page, with an option to use Azure Active Directory, when clicked on, it throws Error loading external login information. and nothing happens.

After tinkering with it for a bit, I've noticed that if I remove this line of code

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();

from ConfigureServices in Startup.cs, it starts working again.

What is it about that line of code that interferes with AzureAD?

标签: .netazure-active-directoryblazorblazor-server-sideasp.net-core-3.0

解决方案


使用代码将 ASP.NET Core Identity 添加到应用程序中,Azure AD 是外部身份验证提供程序之一。

在这种情况下,您可以设置CookieSchemeName为,Identity.External以便 asp.net 核心身份可以从外部身份提供者获取外部用户配置文件:

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "xxx.onmicrosoft.com",
    "TenantId": "xxx-a2dd-4fde-bf8f-f75ab18b21ac",
    "ClientId": "xxxxx-9f22-4c88-aafb-fe00a30caa78",
    "CallbackPath": "/signin-oidc",
    "CookieSchemeName": "Identity.External"
},

认证后,默认情况下,asp.net core identity 会创建一个本地账户关联你的外部账户,这样你就可以用你的本地身份系统进行授权。


推荐阅读