asp.net – WebForms身份验证作为MVC过滤器
发布时间:2020-12-16 09:41:39 所属栏目:asp.Net 来源:网络整理
导读:在从WebForms到MVC的迁移中,一些.aspx页面仍然存在.这些的身份验证当前是基于文件的,并通过Web.config进行.通过向GlobalFilters.Filters添加AuthorizeAttribute并根据需要向控制器/操作添加MVC身份验证. Web.config身份验证目前看起来像这样: authenticatio
|
在从WebForms到MVC的迁移中,一些.aspx页面仍然存在.这些的身份验证当前是基于文件的,并通过Web.config进行.通过向GlobalFilters.Filters添加AuthorizeAttribute并根据需要向控制器/操作添加MVC身份验证.
Web.config身份验证目前看起来像这样: <authentication mode="Forms">
<forms loginUrl="~/SignIn.aspx" protection="All" path="/" timeout="10080" />
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
<location path="SomePage.aspx">
<system.web>
<authorization>
<allow roles="Administrator,BasicUser" />
<deny users="*" />
</authorization>
</system.web>
</location>
但是,我想将WebForms身份验证从Web.config转移到使用方法调用执行检查的MVC过滤器.我一直无法找到这方面的一个例子.这是否可能,是否有任何不良影响? 我知道.aspx文件默认不由MVC钩子处理. 我正在寻找一种方法来获取所有.aspx文件,无论他们的代码隐藏的基类是什么. 解决方法
在Global.asax.cs中,
protected void Application_AuthenticateRequest(object sender,EventArgs e)
{
// Since MVC does not handle authentication for .aspx pages,do this here.
AuthController.AuthenticateAspxPage(this.Context);
}
在AuthController中,对于某些类型的UserRole, public const string ErrorRedirectUrl = "~/Auth/Error";
private static readonly IDictionary<string,UserRole[]> _aspxAccessRoles =
new Dictionary<string,UserRole[]>()
{
{ "~/SomePage.aspx",new UserRole[] { UserRole.Administrator,UserRole.BasicUser } },...
};
然后, [NonAction]
public static void AuthenticateAspxPage(HttpContext context)
{
string ext = context.Request.CurrentExecutionFilePathExtension;
string aspxPage = context.Request.AppRelativeCurrentExecutionFilePath;
ClaimsPrincipal principal = context.User as ClaimsPrincipal;
if ((ext == ".aspx" || ext == ".ashx") && !HasAspxAccess(aspxPage,principal))
{
context.Response.Redirect(ErrorRedirectUrl);
}
}
[NonAction]
public static bool HasAspxAccess(string aspxPage,ClaimsPrincipal principal)
{
if (principal == null || principal.Claims == null)
{
return false;
}
string[] userRoles = principal.Claims
.Where(claim => claim.Type == ClaimTypes.Role)
.Select(claim => claim.Value)
.ToArray();
UserRole[] accessRoles;
return _aspxAccessRoles.TryGetValue(aspxPage,out accessRoles)
&& accessRoles.Any(role => userRoles.Contains(role.ToString()));
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 根据Web API请求设置JSON CamelCase
- asp.net – 在没有子类化页面的情况下全局覆盖Page_PreInit
- asp.net-mvc – 如何将模型中的占位符文本添加到MVC视图中?
- asp.net-mvc-3 – BreadCrumb- ASP.NET MVC3
- 指定ASP.NET Http Handler的确切路径
- asp.net-mvc – 编译时mvc视图检查与msbuild
- 通过asp.net 4.0中的“EnableViewState”和“ViewStateMode
- asp.net-mvc-2 – 为什么我们在ASP.NET MVC中使用HTML助手?
- asp.net-mvc-3 – 在MVC 3实体框架中添加多个到多个链接表的
- Asp.net,paypal和IPN ..如何自动付款?
推荐文章
站长推荐
- asp.net-mvc – 如何使用Moq测试一个自定义的Mod
- asp.net – 工作线程和I/O线程有什么区别?
- asp.net – 有没有办法禁用整个页面的事件验证?
- asp.net-mvc – 可以使用“Bundle.Include”(在A
- asp.net-mvc – 可扩展的SignalR Azure – 在哪里
- 请求的ASP.NET窗体身份验证失败 原因:提供的票已
- asp.net – 如何指定相对于站点根目录的路径?
- asp.net – Visual Studio 2012 – 哪里有ASPX设
- asp.net-mvc-3 – ViewBag在Extension Class中返
- asp.net-mvc – 跨应用程序进行表单身份验证的Se
热点阅读
