Ala*_*ock 10 asp.net security authentication asp.net-mvc forms-authentication
设置(使用MVC 4)
public class MyAuthorizeAttribute : AuthorizeAttribute {
protected override bool AuthorizeCore(HttpContextBase httpContext) {
var isAuthorised = base.AuthorizeCore(httpContext);
if(isAuthorised) {
// retrieve authentication ticket from cookie and
// create custome principal and attach to
// httpContext.User
}
return isAuthorised;
}
}
Run Code Online (Sandbox Code Playgroud)
Gloabl.asax.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new MyAuthorizeAttribute());
}
Run Code Online (Sandbox Code Playgroud)
HomeController.cs:
using System.Web.Mvc;
public class HomeController : Controller
{
[AllowAnonymous]
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
问题
对主页的调用会强制加载登录页面.
题
当HomeController.Index()动作用[AllowAnonymous]修饰时,为什么ASP会将我重定向到登录视图?
我正在使用这篇文章作为参考
Ala*_*ock 11
根据我对原始问题的评论.问题是索引视图是在返回部分视图的其他控制器上调用操作.只是通过一切并剥离旧的[Authorize]属性的情况.
虽然原始海报在他的案例中找到了原因,但我想分享我的决议,因为我在遇到相同的症状时遇到了这个问题.
在我的web.config文件中,遵循webforms的逻辑:
<authorization>
<deny users="?" />
</authorization>
Run Code Online (Sandbox Code Playgroud)
您必须没有这个,因为它会阻止请求在没有先登录的情况下执行任何操作,除了重定向发生的登录操作.当我尝试添加第二个公共动作时,我才发现了这一点.
| 归档时间: |
|
| 查看次数: |
9149 次 |
| 最近记录: |