我有一個FubuMvc網站,它使用來自WIF單點登錄服務器的基於聲明的授權。身份驗證發生在SSO上,包括角色和一組自定義聲明的聲明被傳遞到網站以進行授權。根據索賠允許或拒絕訪問整個站點
SSO和網站正常工作,但基於角色的授權,但我想要做的是拒絕訪問整個網站保存錯誤頁面的基礎上沒有自定義聲明。目前我正在使用自定義ClaimsAuthenticationManager
,它檢查索賠並檢查是否存在所需索賠。如果聲明丟失,則會引發異常。這會導致500錯誤,但我真正想要的是系統拋出401錯誤並重定向到網站上的未授權頁面。
以下是自定義ClaimsAuthenticationManager
的示例。
public class CustomAuthenticationManager : ClaimsAuthenticationManager
{
private readonly string expectedClaim;
public CustomAuthenticationManager (object config)
{
var nodes = config as XmlNodeList;
foreach (XmlNode node in nodes)
{
using (var stringReader = new StringReader(node.OuterXml))
using (var rdr = new XmlTextReader(stringReader))
{
rdr.MoveToContent();
rdr.Read();
string claimType = rdr.GetAttribute("claimType");
if (claimType.CompareTo(ClaimTypes.CustomClaim) != 0)
{
throw new NotSupportedException("Only custom claims are supported");
}
expectedSystemName = rdr.GetAttribute("customClaimValue");
}
}
}
public override IClaimsPrincipal Authenticate(
string resourceName, IClaimsPrincipal incomingPrincipal)
{
var authenticatedIdentities = incomingPrincipal.Identities.Where(x => x.IsAuthenticated);
if (authenticatedIdentities.Any() &&
authenticatedIdentities.Where(x => x.IsAuthenticated)
.SelectMany(x => x.Claims)
.Where(x => x.ClaimType == ClaimTypes.CustomClaim)
.All(x => x.Value != expectedClaim))
{
throw new HttpException(
(int)HttpStatusCode.Unauthorized,
"User does not have access to the system");
}
return base.Authenticate(resourceName, incomingPrincipal);
}
}
上述工作,並阻止對系統的訪問,但TI並不十分友好的使用,因爲用戶只是得到一個500錯誤。此外,因爲用戶基本上登錄了他們但沒有訪問權限,他們無法註銷。我公開了一個未經授權的錯誤頁面,該頁面提供對匿名用戶的訪問,但我無法重定向用戶。
我已經驗證過該答案是正確的。授權必須在'ClaimsAuthorizationManager'中完成。從'CheckAccess'返回'false'是獲得401響應的唯一方法。 –