c# - 更新 ClaimsPrincipal 中的声明

标签 c# azure-active-directory adal

我将 Adal 与 Azure Active Directory 结合使用,我需要通过自定义 OwinMiddleware 添加额外声明。 当我向该委托(delegate)人添加声明时,我可以在当前请求中访问它们。但在页面刷新后,声明消失了。

我以为 Owin 处理了声明的序列化并将其放入 cookie 本身,但事实似乎并非如此。

我添加声明如下:

 var claimsIdentity = (ClaimsIdentity) ClaimsPrincipal.Current.Identity;
        if (!claimsIdentity.IsAuthenticated) return;

        var identity = new ClaimsIdentity(claimsIdentity);

        var currentTenantClaim = GetTenantClaim();

        if (currentTenantClaim != null)
            claimsIdentity.RemoveClaim(currentTenantClaim);

        claimsIdentity.AddClaim(new Claim(ClaimTypes.CurrentTenantId, id));

        context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant
            (new ClaimsPrincipal(identity), new AuthenticationProperties {IsPersistent = true});

关于如何保留对 cookie 的新声明有什么想法吗?

最佳答案

我已将声明添加到错误的身份。它们必须添加到标识变量而不是 claimsIdentity。

工作代码:

        var claimsIdentity = (ClaimsIdentity) context.Authentication.User.Identity;
        if (!claimsIdentity.IsAuthenticated) return;

        var identity = new ClaimsIdentity(claimsIdentity);

        var currentTenantClaim = GetTenantClaim(identity);

        if (currentTenantClaim != null)
            identity.RemoveClaim(currentTenantClaim);

        identity.AddClaim(new Claim(ClaimTypes.CurrentTenantId, id));

        context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant
            (new ClaimsPrincipal(identity), new AuthenticationProperties {IsPersistent = true});

关于c# - 更新 ClaimsPrincipal 中的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40660233/

相关文章:

c# - 如何使用 linq toEntity 在单个查询中获取两个列值

powershell - 在 PowerShell 中获取 Azure Active Directory 密码到期日期

ios - 针对 iOS 上的 Azure Graph API 的非交互式登录(缓存凭据)

c# - 根据值更改 Silverlight4 中文本 block 的前景色

c# - 如何使用依赖注入(inject)从多个来源获取配置?

azure - 如何使用 REST API 登录 Azure 门户

python - 查询 OneDrive 中的文件返回空列表

adal - 在 Active Directory 中,什么是资源?

c# - 具有 IntegralHeight 的 ComboBox 的默认 DropDown 高度为 TRUE

python - 为什么 Azure Active Directory OAuth 适合我而不适合其他人?