C# 如何在Windows身份基础(WIF)声明中添加角色

C# 如何在Windows身份基础(WIF)声明中添加角色,c#,asp.net,webforms,wif,claims-based-identity,C#,Asp.net,Webforms,Wif,Claims Based Identity,我需要将STS(安全令牌服务)添加到ASP.NET Webforms网站。主要问题是,我需要在进行身份验证后添加角色声明,因为身份提供者没有角色信息 我已经在本地STS中实现了CustomSecurityTokenService,使用类似于下面的代码。这段代码按预期工作-但是,我需要在流程后面的注释“GetRoles for user here”下添加该位 // Get the incoming identity IClaimsIdentity callerId

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

我已经在本地STS中实现了
CustomSecurityTokenService
,使用类似于下面的代码。这段代码按预期工作-但是,我需要在流程后面的注释“GetRoles for user here”下添加该位

        // 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>
我使用的是.NET4.0——虽然我知道升级到4.5可能会有一些好处,但目前对我来说这不是一个选择


我在这上面花了很长时间,所以感谢您的帮助

我在查看和之后找到了答案

我最初没有得到的一点是,为了使用
ClaimsAuthenticationManager
,您需要在
部分的web.config中添加额外的行来连接实现

因此:


我想知道为什么我的代码从来没有被执行过

var currentUser = Service.GetUser(callerIdentity.Name);
foreach (var role in currentUser.Roles) 
{
           outputIdentity.Claims.Add(new Claim(ClaimTypes.Role, role));
}
  <claimsAuthenticationManager type="MyCustomClaimsAuthenticationManager" />