Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在使用Azure B2C身份验证服务进行身份验证之前验证电子邮件_C#_Asp.net_Azure_Openid_Azure Ad B2c - Fatal编程技术网

C# 在使用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

形势

我想为我的web应用程序使用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"
        },
    };
}

非常感谢您的帮助

您必须在“选择应用程序声明”刀片中的策略中指定要发送到应用程序的声明。

您可以配置此功能的刀片服务器是

Azure AD B2C设置>设置>注册或登录策略>应用程序声明

您应该确保已根据下面的屏幕截图选择了
电子邮件


如果是B2C租户,您是否在B2C管理门户中选择“电子邮件”作为应用程序声明之一?