【发布时间】:2022-01-07 19:35:57
【问题描述】:
我正在尝试在运行时从我的数据库中添加一些自定义声明。
我将这篇文章作为我工作的基础: Add Arbitrary Claims after AzureAd Login
当我在 VS 2019 中本地运行时,该代码运行良好。我可以打开多个隐身窗口,登录不同的用户,并且每次都按预期触发 OnTokenValiated 事件。行为是:
- Azure AD 登录
- OnTokenValidated
- 主页
我已将其发布为 Azure Web 应用程序,并且该事件永远不会触发。发布后,如果我去那个网站的主页,行为是:
- Azure AD 登录
- 转到主页
我什至发布了一个除以零错误(未处理)的代码,它永远不会触发。
我现在很茫然,想知道 Azure 配置中是否缺少我的设置或其他什么?
这是我的代码的净化版本。
services
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
Configuration.Bind("AzureAd", options);
options.Events ??= new OpenIdConnectEvents();
var onTokenValidated = options.Events.OnTokenValidated;
options.Events.OnTokenValidated = ctx =>
{
onTokenValidated?.Invoke(ctx);
var userId = "";
if (ctx.Principal.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier") != null)
{
userId = ctx.Principal.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");
}
else if (ctx.Principal.FindFirstValue("http://schemas.microsoft.com/identity/claims/objectidentifier") != null)
{
userId = ctx.Principal.FindFirstValue("http://schemas.microsoft.com/identity/claims/objectidentifier");
}
ResponseClass userConfig = AccountDataAccess.GetUserConfig(
UserId: userId
);
if (userConfig.Success)
{
newClaims.Add(new Claim("SessionId", userConfig.SessionId));
newClaims.Add(new Claim("Permission1", userConfig.Permission1));
}
var appIdentity = new ClaimsIdentity(newClaims);
ctx.Principal.AddIdentity(appIdentity);
return Task.CompletedTask;
};
});
任何帮助将不胜感激。
【问题讨论】:
-
你确定redirectURL,在你验证后是正确的并且可以在生产中工作吗?我会查看日志,找出失败的原因.. 您是否在生产中获得了经过身份验证的用户对象?
-
嗨 Tore - 我在这里使用了视频 link 并故意中断了我的回调。我没有收到那个错误。你会看哪些日志?因为它没有在我期望的地方触发事件代码,所以我不确定从哪里开始。我注意到的最后一件事:在视频中,使用本地主机,redirect_uri 结束看起来像 myapp.azurewebsites.net/signin-oidc 但是当我查看我的应用程序创建的链接时,它看起来像:myapp.azurewebsites.net/.auth/login/aad/callback
标签: c# authentication .net-core azure-web-app-service openid-connect