C# 在使用Azure B2C身份验证服务进行身份验证之前验证电子邮件
形势 我想为我的web应用程序使用Azure B2C身份验证服务。但是,我希望应用程序管理员限制对某些电子邮件或域的访问,例如白名单,如下所示:C# 在使用Azure B2C身份验证服务进行身份验证之前验证电子邮件,c#,asp.net,azure,openid,azure-ad-b2c,C#,Asp.net,Azure,Openid,Azure Ad B2c,形势 我想为我的web应用程序使用Azure B2C身份验证服务。但是,我希望应用程序管理员限制对某些电子邮件或域的访问,例如白名单,如下所示: tom1@abc.com tom2@def.com *@alphabet.com 因此,只有排名前两位的电子邮件和其他电子邮件以“alphabet.com”结尾的人才能访问该网站 问题 我已经实现了所有功能,并且工作正常,但是我正在努力获取经过身份验证的用户的电子邮件地址,以便在登录/登录过程中进行白名单检查。AuthenticationTicke
- tom1@abc.com
- tom2@def.com
- *@alphabet.com
Startup.App.cs中的代码
private OpenIdConnectAuthenticationOptions CreateOptionsFromPolicy(string policy)
{
return new OpenIdConnectAuthenticationOptions
{
// For each policy, give OWIN the policy-specific metadata address, and
// set the authentication type to the id of the policy
MetadataAddress = String.Format(aadInstance, tenant, policy),
AuthenticationType = policy,
// These are standard OpenID Connect parameters, with values pulled from web.config
ClientId = clientId,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = AuthenticationFailed,
AuthorizationCodeReceived = (context) =>
{
// no claims present here
return Task.FromResult(0);
},
SecurityTokenReceived = (context) =>
{
// no claims present here
return Task.FromResult(0);
},
SecurityTokenValidated = (context) =>
{
// print all claims - quite a few except email :( Is this where this check should be done?
foreach (var claim in context.AuthenticationTicket.Identity.Claims)
{
Console.WriteLine(claim.Value);
}
return Task.FromResult(0);
},
},
Scope = "openid",
ResponseType = "id_token",
// This piece is optional - it is used for displaying the user's name in the navigation bar.
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
},
};
}
非常感谢您的帮助 您必须在“选择应用程序声明”刀片中的策略中指定要发送到应用程序的声明。
您可以配置此功能的刀片服务器是
Azure AD B2C设置>设置>注册或登录策略>应用程序声明
您应该确保已根据下面的屏幕截图选择了电子邮件
如果是B2C租户,您是否在B2C管理门户中选择“电子邮件”作为应用程序声明之一?