我有一个使用AzureAD身份验证的asp.net核心Web应用程序。 当我进行身份验证时,将触发OnAuthorizationCodeReceived事件,并包含有效的Principal。但是,在我的控制器处理程序被调用时,该Principal已丢失(User.Identity只是一个未经身份验证的空用户)。如果我调用带有Authorize标记的方法,浏览器将进入无休止的重定向循环,尝试反复进行身份验证。
我怀疑问题与身份和webapp用户配置文件之间的映射有关,但我不知道从哪里开始进行故障排除或如何中断获取用户配置文件的代码。不管发生什么故障,它都会默默地执行此操作,而不会指出发生了什么问题。
一个问题是,该Web应用程序过去曾工作过,但仅转发了我需要的部分声明(我需要它们来认证MS Graph客户端)。我将尝试汇总导致成功身份验证的条件,并在此处进行更新。
这是我的Startup.cs:ConfigureServices中的相关代码:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddAzureAd(options => Configuration.Bind("AzureAd", options));
...
services.AddScoped<UserManager<IdentityUser>, UserManager<IdentityUser>>();
排除appid / secret的身份验证选项:
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"CallbackPath": "/signin-oidc",
"BaseUrl": "https://localhost:44327",
"Scopes": "openid email profile",
"GraphResourceId": "https://graph.microsoft.com/",
"GraphScopes": "User.Read User.ReadBasic.All Mail.Send Device.Read Device.Command Files.ReadWrite",
"SaveTokens": true,
"ResponseType": "token id_token"
}
我的AzureAD设置类似于此处的图形示例:https://github.com/microsoftgraph/aspnetcore-connect-sample/tree/master/MicrosoftGraphAspNetCoreConnectSample
但是该示例未使用用户个人资料,这可能是我遇到问题的地方。
欢迎进行故障排除/跟踪建议。