我花了一些时间让我的 MVC 6 .NET Core 网站与 Azure B2C 一起工作,一切似乎都运行良好。但是,围绕声称我似乎无法找出正确策略的一些问题。
假设用户使用电子邮件、名字、姓氏在我的网站上注册。注册完成后,我想将一条记录添加到我的数据库中引用该用户的 UserProfile 表中。
问题 1: 我应该在 Azure B2C 中创建“UserProfileId”声明吗?或者我应该在我的数据库表中创建一个引用 AD 用户的“ObjectId”字段?什么更有意义?
问题 2: 一旦用户注册,我将在哪里以及如何更新 AD 用户声明?我会在这些事件之一中这样做吗?或者别的地方?我看到有一个“用户是新用户”声明可以检查?
OnAuthenticationValidated
OnAuthorizationCodeReceived
OnRedirectToAuthenticationEndpoint
问题 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 });
});
}