使用Azure B2C身份验证服务进行身份验证之前验证电子邮件

时间:2016-08-09 08:43:04

标签: c# asp.net azure openid azure-ad-b2c

场合

我想为我的网络应用程序使用Azure B2C身份验证服务。但是,我希望应用的管理员限制对某些电子邮件或域的访问,例如白名单如下:

  • tom1@abc.com
  • tom2@def.com
  • * @ alphabet.com

所以只有前两封电子邮件和其他任何人以电子邮件结尾于" alphabet.com"可以访问该网站。

问题

我已经实施了所有内容并且工作正常,但是我很难获得经过身份验证的用户的电子邮件地址,以便在登录/登录过程中进行白名单检查。 AuthenticationTicket具有所请求的所有声明(FirstName,LastName,Name,Object Identifer等),但电子邮件不存在(它已在Azure B2C中设置为声明)。

如何访问电子邮件,这是正确的检查位置?

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"
        },
    };
}

任何帮助非常感谢。

1 个答案:

答案 0 :(得分:0)

您必须在“选择应用程序声明”广告素材中的政策中指定要发送给您的应用程序的声明。

您可以配置它的刀片是

Azure AD B2C设置>设置>注册或登录政策>申请索赔

您应该确保根据以下屏幕截图选择了email

Select application claims