MVC如何获取Web.config的授权节点以使用elmah的ADFS索赔
-
21-12-2019 - |
题
我有一个现有的MVC应用程序,它使用ADFS 2.0发出索赔。
索赔由数据库发出而不是Active Directory组,因此不使用索赔级别命名空间(我可能会编辑此操作,以便他们这样做)。
在任何情况下,我需要编写某种转换(可能在一个HttpModule中,因为我认为在请求过程中的动作过滤器将太晚),这将采用“My-namespace:管理员”,如果存在和存在将其转化为可以在Web配置的Elmah部分中测试的角色。我假设我所需要的只是让索赔与索赔的IsInrole方法合作<location path="elmah">
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*"/>
</authorization>
</system.web>
.
或者是更容易设置路由约束,检查我目前从ADF返回的索赔
解决方案
我最终写了一个动作过滤器
public class ElmahRequestAuthorizationFilter : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.IsChildAction) return;
var controller = filterContext.RouteData.Values["controller"] as string;
if (controller != null && controller.ToLowerInvariant() != "elmah") return;
var authenticationComponent = GetAuthenticationInfo() // A method that will return us roles;
var goodRoles = new List<string> {
"TestRole",
"ThirdLevelSupport",
"Administrator"
};
var roles = authenticationComponent.Roles ?? new List<string>();
var thouShaltPass = roles.Intersect(goodRoles).Any();
if (!thouShaltPass)
{
throw new HttpException(404, "Not Found");
}
}
}
. 不隶属于 StackOverflow