我有一个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响应的唯一方法。 –