【发布时间】: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