【问题标题】:How to add roles to Windows Identity Foundation (WIF) claim如何将角色添加到 Windows Identity Foundation (WIF) 声明
【发布时间】:2014-04-14 19:46:23
【问题描述】:

我需要将 STS(安全令牌服务)添加到 ASP.NET Webforms 网站。主要问题是我需要在身份验证发生后为角色添加声明,因为身份提供者没有角色信息。

我已经使用类似于下面的代码在本地 STS 中实现了CustomSecurityTokenService。此代码按预期工作 - 但是,我需要在稍后的过程中在“获取用户的角色”注释下添加该位..

        // Get the incoming identity 
        IClaimsIdentity callerIdentity = (IClaimsIdentity)principal.Identity;

        // Issue custom claims.
        ClaimsIdentity outputIdentity = new ClaimsIdentity("Federation");

        var username = callerIdentity.Name;
        var domain = "DOMAIN";

        outputIdentity.Claims.Add(new Claim(ClaimTypes.Name, callerIdentity.Name));
        outputIdentity.Claims.Add(new Claim(ClaimTypes.WindowsAccountName, string.Format("{0}\\{1}", domain, username)));


        // get roles for user here:
        var roles = "Admin"; 

        string[] rolelist = roles.Split(',');

        foreach (var role in rolelist)
        {
            outputIdentity.Claims.Add(new Claim(ClaimTypes.Role, role));
        }

        return outputIdentity;

我遇到的问题是,我目前无法获取角色,因为它们是由身份提供者之外的服务提供的。我需要等到我回到 RP(应用程序)才能获得它 - 但到那时,由于我的角色设置,web.config 中的安全设置已将我锁定。

 <location path="Pages/Secure/Messages/Default.aspx">
        <system.web>
            <authorization>
                <allow roles="Admin, Tecchies"/>
            </authorization>
        </system.web>
    </location>

我认为我可以在 global.asax 中使用一个事件,但到目前为止我还没有找到任何选项来工作。像这样的代码:

var currentUser = Service.GetUser(callerIdentity.Name);
foreach (var role in currentUser.Roles) 
{
           outputIdentity.Claims.Add(new Claim(ClaimTypes.Role, role));
}

我正在使用 .NET 4.0 - 虽然我知道升级到 4.5 可能会有一些好处,但目前这不是我的选择。

我已经为此花费了很长时间,因此感谢您提供任何帮助!

【问题讨论】:

    标签: c# asp.net webforms wif claims-based-identity


    【解决方案1】:

    在查看this linkthis link 之后,我最终找到了答案。

    我最初没有得到的一件事是,为了使用 ClaimsAuthenticationManager,您需要在 web.config 的 &lt;microsoft.identityModel&gt;&lt;services&gt; 部分添加额外的行以连接实现。

    因此:

      <claimsAuthenticationManager type="MyCustomClaimsAuthenticationManager" />
    

    我想知道为什么我的代码从未被执行..

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 2016-02-06
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 2016-02-28
      相关资源
      最近更新 更多