azure - 获取 Azure AD B2C token 时出错

标签 azure asp.net-core azure-ad-b2c razor-pages

我使用 Azure AD B2C 来满足我的 Web 应用程序的用户身份要求。我正在使用 ASP.NET Core 2.0 Razor Pages 项目来构建 Web 应用程序。

如果用户在尝试访问网页时未经过身份验证,他们将被路由到 Azure AD B2C 登录页面。如果身份验证成功,它们就会返回到我的网络应用程序。

我正在尝试使用此文档 https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-access-tokens 获取返回的 JWT token

我不断收到以下错误:

enter image description here

我正在使用以下代码来检索 token 。

string url = $"https://login.microsoftonline.com/{this._options.Domain}/oauth2/v2.0/authorize?";
            string policy = this._options.SignUpSignInPolicyId;
            string clientid = this._options.ClientId;
            string grantType = "authorization_code";
            Random random = new Random();
            int nonce = random.Next();
            string redirectUri = WebUtility.UrlEncode("https://localhost:44315/");
            string scope = WebUtility.UrlEncode($"https://{this._options.Domain}/notes/openid");
            const string responseType = "code";

            string request =
                $"{url}p={policy}" +
                $"&client_id={clientid}" +
                $"&grant_type={grantType}" +
                $"&nonce={nonce}" +
                $"&redirect_uri={redirectUri}" +
                $"&scope={scope}" +
                $"&response_type={responseType}";

            var response = await GetData(request);

最佳答案

从 Azure AD B2C 返回的声明似乎包含在 User.Claims 对象中。

if (User.Identity.IsAuthenticated)
{
    Claim emailClaim = User.Claims.FirstOrDefault(claim => claim.Type == "emails");
    Claim isNewClaim = User.Claims.FirstOrDefault(claim => claim.Type == "newUser");
}

关于azure - 获取 Azure AD B2C token 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49278568/

相关文章:

azure - Blob CDN 终结点的自定义子域名导致错误 400

linux - 如何在 linux 服务器上使用 pm2 运行 asp.net core 应用程序

oauth-2.0 - Azure AD B2C : User. Identity.Name 为 null,但 User.Identity.m_instance_claims[9] 具有名称

azure - 有没有办法恢复因长度过长而被截断的完整 Application Insights 消息?

azure-ad-b2c - 将声明添加到 token Azure B2C

sql-server - Azure ADF V2 - 完成时的事件

Azure 数据工厂中的 XML 验证

Azure ARM模板“找不到名称为hostingEnvironment

c# - 如何在 ASP.NET Core 中的子域之间共享 session ?

asp.net - 在 ASP.Net Core 2.2 中定义不同的 API 以接受不同的大小限制