c# - 添加 Azure Ad Oauth2 JWT token 声明

标签 c# oauth-2.0 jwt azure-active-directory claims

我只是想知道是否有办法通过 Azure 门户向 Azure Ad OAuth2 JWT token 添加或指定自定义声明? 或者这是唯一可能的代码方面?

最佳答案

据我所知,Azure AD目前不支持发出自定义声明。

作为解决方法,我们可以使用 Azure AD Graph 添加 directory schema extensions .之后,我们可以使用 Azure AD Graph 获取数据扩展,并在验证安全 token 时添加自定义声明,如下代码所示:

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        Authority = authority,
        PostLogoutRedirectUri = postLogoutRedirectUri,
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
            AuthenticationFailed = context => 
            {
                context.HandleResponse();
                context.Response.Redirect("/Error?message=" + context.Exception.Message);
                return Task.FromResult(0);
            }
            ,
            SecurityTokenValidated = context =>
            {
                //you can use the Azure AD Graph to read the custom data extension here and add it to the claims 
                context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim("AddByMe", "test"));
                return Task.FromResult(0);
            }
    });

此外,如果您对 Azure 有任何想法或反馈,可以通过 here 提交。 .

关于c# - 添加 Azure Ad Oauth2 JWT token 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42434254/

相关文章:

c# - Instagram OAuth 2.0 授权(注册重定向 URI)。我还应该注册 WWW.* 版本的 URI 吗?

django - 删除过期的访问 token

c# - 从本地机器商店中的证书创建 CngKey 以用于 ECDsaCng(CngKey)?

c# - 如何在Azure表存储中使用RowKey或时间戳检索最新记录

c# - 无法在 C# 中的静态类中声明实例成员

c# - ASP.NET初学者问题: Need help regarding using some class library

c# - 在 C# 中从 MSSQL 输出列的内容

android - 浏览器 Intent 并返回正确的 Activity (关闭打开的选项卡)

javascript - 如何在 Angular JS 中使用 JWT 调用 REST API

spring-boot - Spring Boot 和 spring-security-jwt 的依赖