gpt4 book ai didi

asp.net - 将自定义 RoleProvider 与 Windows Identity Foundation - STS 结合使用

转载 作者:行者123 更新时间:2023-12-01 11:02:59 24 4
gpt4 key购买 nike

我创建了执行身份验证部分的 STS。它使用自定义成员资格提供程序。成功登录后,我被重定向到我的 RP 网站。在身份验证方面一切正常。

我已经在我的 RP 网站的 web.config 中定义了一个 CustomRolesProvider。它使用 STS 返回的用户名从 RP 的数据库中获取该用户的角色。当我使用 Roles.GetRolesForUser 时,我确实获得了正确的角色。

我在我的 RP 的 web.config 中有以下内容,只允许管理员授予对管理文件夹的访问权限。

站点地图提供者有 securityTrimmingEnabled="true"

<location path="admin">
<system.web>
<authorization>
<allow roles="admin" />
<deny users="*" />
</authorization>
</system.web>
</location>

<add name="default" type="System.Web.XmlSiteMapProvider" siteMapFile="Web.sitemap" securityTrimmingEnabled="true" />

问题:当用户处于管理员角色时,管理页面的菜单选项卡将不会显示。我确实检查过 Roles.IsUserInRole("admin") 是否返回 true。因此角色被角色提供者识别,但不被 web.config 中的授权规则和站点地图提供者识别。

如果我从 web.config 中注释掉“位置”,即允许每个登录用户访问管理文件夹,我的菜单项就会正常显示。

根据我对 WIF 的理解,RP 可以拥有自己的 Roles 实现,而不必依赖于 STS 的 Roles Claim。

有没有人有什么想法?

更新 2(01/20/2012):我发现 STS 返回的角色声明如下:

http://schemas.microsoft.com/ws/2008/06/identity/claims/role = Manager

所以如果我改变 <allow roles="admin" /> to <allow roles="Manager" />角色被拾取并且菜单选项卡被适本地显示。

所以我确定我缺少关于如何使用我的角色的链接,而不是通过声明返回的链接。

更新 2(01/20/2012):如果我像下面那样将角色添加到 claimsIdentity 中,它会起作用:

void Application_AuthenticateRequest(object sender, EventArgs e) {
if (Request.IsAuthenticated) {
IClaimsPrincipal claimsPrincipal = HttpContext.Current.User as IClaimsPrincipal;
IClaimsIdentity claimsIdentity = (IClaimsIdentity)claimsPrincipal.Identity;
if (!claimsIdentity.Claims.Exists(c => c.ClaimType == ClaimTypes.Role))
{
claimsIdentity.Claims.Add(new Claim(ClaimTypes.Role, "admin"));
}
}
}

但是添加该代码的最佳位置是什么?如果我将它添加到 Application_AuthenticateRequest 中,它会根据每个请求添加,并且会不断添加。(我通过添加 if 语句修复了此问题)

*更新 3(01/24/2012):*我的代码的版本 2 使用我的 CustomRoleProvider 获取角色,然后将其添加到 ClaimsCollection:

void Application_AuthenticateRequest(object sender, EventArgs e) {
if (Request.IsAuthenticated) {
string[] roleListArray = Roles.GetRolesForUser(User.Identity.Name);
IClaimsPrincipal claimsPrincipal = HttpContext.Current.User as IClaimsPrincipal;
IClaimsIdentity claimsIdentity = (IClaimsIdentity)claimsPrincipal.Identity;
var roleclaims = claimsIdentity.Claims.FindAll(c => c.ClaimType == ClaimTypes.Role);
foreach (Claim item in roleclaims)
{
claimsIdentity.Claims.Remove(item);
}

foreach(string role in roleListArray)
{
claimsIdentity.Claims.Add(new Claim(ClaimTypes.Role, role));
}

HttpContext.Current.User = claimsPrincipal;
}

但我不确定这是不是正确的方法。

有没有人做过这样的事情??

更新 4 (01/26/2012): 发现我可以使用 Custom ClaimsAuthencationManager (第 4 步)转换我的主张。我将 Global.asax 中 AuthenticateRequest 方法中的代码移动到 ClaimsAuthenticationManager 类中的 Authenticate 方法中。

我怀疑它能比这更好。我将发布我的解决方案作为答案。但如果有人有任何其他更好的解决方案,请随时发表评论。

最佳答案

您可以使用自定义的 ClaimsAuthencationManager,但是它会在每次请求时被调用。我的建议是使用 WSFederationAuthenticationModule.SecurityTokenValidated .使用 SecurityTokenValidatedEventArgs 类的 ClaimsPrincipal 属性并使用您的提供商添加角色。此外,您不妨考虑使用 ClaimsIdentity.RoleClaimType,而不是对角色声明类型进行硬编码。 .

查找到的角色将保存在加密的 cookie 中(假设您使用的是默认值)。

关于asp.net - 将自定义 RoleProvider 与 Windows Identity Foundation - STS 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8945893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com