我花了一些时间让我的MVC 6 .NET Core网站与Azure B2C一起工作,一切似乎都很有效 . 但是,有一些问题围绕着我似乎无法弄清楚正确策略的说法 .
假设用户使用电子邮件,名字,姓氏在我的网站上注册 . 注册完成后,我想将记录添加到我的数据库中引用该用户的UserProfile表中 .
Question 1: 我应该在Azure B2C中创建"UserProfileId"声明吗?或者我应该在数据库表中创建一个引用AD用户的"ObjectId"字段?什么会更有意义?
Question 2: 用户注册后,我在何处以及如何更新AD用户声明?我会在其中一个活动中做到吗?或者别的地方?我看到有一个"User is new"声称我可以检查?
OnAuthenticationValidated
OnAuthorizationCodeReceived
OnRedirectToAuthenticationEndpoint
Question 3: 要更新声明,我可以使用:Microsoft.Azure.ActiveDirectory.GraphClient吗?有没有人有任何关于如何更新自定义声明的示例代码?我似乎仍然存在:
var identity = context.AuthenticationTicket.Principal.Identity as ClaimsIdentity;
identity?.AddClaim(new Claim("EmployeeId", "33"));
这是我的身份验证配置 . 谢谢!!!!!
public void ConfigureAuth(IApplicationBuilder app, IOptions<PolicySettings> policySettings, AuthenticationHelper authHelper)
{
app.UseCookieAuthentication(options =>
{
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.AccessDeniedPath = "/Home/Forbidden";
options.CookieSecure = CookieSecureOption.Always;
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.SlidingExpiration = true;
});
app.UseOpenIdConnectAuthentication(options =>
{
options.PostLogoutRedirectUri = policySettings.Value.PostLogoutRedirectUri;
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.ClientId = policySettings.Value.ClientId;
options.CallbackPath = new PathString("/signin-mysite");
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.ResponseType = OpenIdConnectResponseTypes.IdToken;
options.Authority = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", policySettings.Value.AadInstance, policySettings.Value.Tenant);
options.Events = new OpenIdConnectEvents {
OnAuthenticationValidated = OnAuthenticationValidated,
OnAuthorizationCodeReceived = OnAuthorizationCodeReceived,
OnAuthenticationFailed = OnAuthenticationFailed,
OnRedirectToAuthenticationEndpoint = OnRedirectToAuthenticationEndpoint
};
options.ConfigurationManager = new PolicyConfigurationManager(
String.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}/{3}", policySettings.Value.AadInstance, policySettings.Value.Tenant, "v2.0", OpenIdProviderMetadataNames.Discovery),
new string[] { policySettings.Value.SignUpInPolicyId, policySettings.Value.ProfilePolicyId, policySettings.Value.PasswordPolicyId });
});
}
2 回答
1a - 我没有给B2C租户添加任何东西 .
1b - 我从B2C获取对象id并将其作为备用密钥存储在我的表中 . 我的 table 有一个独特的id . 如果我希望有其他身份提供者,这将是必要的 .
我只使用B2C中的对象ID来查找用户并获得我自己的id .
当您说“更新声明”时,您是指在B2C租户中永久更新,还是指将其添加到其他声明并在此特定令牌的生命周期内暂时使用?
没有使用图形客户端,没有连接回B2C .
userIsNew声明来自B2C一次,仅在注册过程结束时 . 您可以使用它来确定是否有新用户尝试访问您的系统 . 我勾选了从B2C给我的声明中创建表中的新条目,从那时起,声明都来自我表中的信息 .
我不得不再次询问“更新”问题 .
您可能正在寻找的是"transform"索赔 . 这通常是在针对cookie的
TicketReceived
事件期间完成的 . 当他们第一次进行身份验证时就会发生这种情况 . (不要与注册混淆 . )我不是那么聪明,但我会告诉你,我花了太多时间试图把它弄好 . 主要是因为有大量的选项,没有人可以告诉你所有正确的项目 . 因此,您只需查看大量信息即可找到您想要的位数 .
我找到了this book(而且它是现在的,他是一个写得非常好的微软人 .
HTH
关于问题1:我和nhwilly做了同样的事情:我将附加信息存储在我的数据库中 .
关于问题2:您可以在OnsigningIn事件中添加声明:
我从Transforming Open Id Connect claims in ASP.Net Core获得了信息 .
关于问题3:我自己没有这样做,但这个链接应该让你开始:https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet
希望有所帮助!