Asp.net mvc 允许匿名调用asp.net mvc 3中的某些操作
我有一个动作叫“忘记密码”。每次匿名者试图检索操作时,他/她都会被重定向到登录页面。下面是我的实现Asp.net mvc 允许匿名调用asp.net mvc 3中的某些操作,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我有一个动作叫“忘记密码”。每次匿名者试图检索操作时,他/她都会被重定向到登录页面。下面是我的实现 public ActionResult ForgotPassword(string UserName) { //More over when i place a breakpoint for the below line //its not even getting here return View("Login"); } 这是我的web.config文件的一部分
public ActionResult ForgotPassword(string UserName)
{
//More over when i place a breakpoint for the below line
//its not even getting here
return View("Login");
}
这是我的web.config文件的一部分
<location path="">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
<location path="Content">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="Scripts">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="Images">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<authentication mode="Forms">
<forms loginUrl="/Home/Login" timeout="5" slidingExpiration="false" />
</authentication>
如果您使用的是ASP.NET MVC4,您可以尝试将allowanonymous属性添加到您的操作中,如下所示:
[AllowAnonymous]
public ActionResult ForgotPassword(string UserName)
{
//More over when i place a breakpoint for the below line
//its not even getting here
return View("Login");
}
有关更多信息,请参阅Jon Galloway的文章:
我假设您在控制器上设置了“Authorize”属性,这将强制每个控制器操作登录。 我建议从控制器中删除该属性,并将其逐个设置为每个操作。
或者升级到MVC 4并使用AllowAnonymous属性。因为您通过使用拒绝所有人使用应用程序
<authorization>
<deny users="?"/>
</authorization>
或者您也可以用[Authorize]
[Authorize]
public class HomeController : Controller
{
...
}
如果您使用的是ASP.NET MVC4,对于需要匿名访问的操作,请使用
AllowAnonymous
属性
[AllowAnonymous]
public ActionResult ForgotPassword() {
//More over when i place a breakpoint for the below line
//its not even getting here
return View("Login");;
}
根据,您不能使用路由或web.config文件保护MVC应用程序。保护MVC应用程序的唯一受支持的方法是对每个控制器应用Authorize属性,并在登录和注册操作上使用新的AllowAnonymous属性。基于当前领域做出安全决策是一件非常糟糕的事情,它会使您的应用程序面临漏洞 来自此链接: 如果您使用的是MVC 3,则无法执行以下操作:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AuthorizeAttribute());
}
为什么它是全局的和异名的属性在MVC 3上不起作用
因此,您需要构建自己的过滤器。它为我工作(MVC3),你可以检查完整的解决方案
您是否使用
Authorize
属性装饰了Controller/Action
,或者注册了全局Authorize
过滤器?AllowAnonymous在MVC 3中起作用吗?我不这么认为。这就是邮局老板的要求。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AuthorizeAttribute());
}
using System.Web.Mvc;
using MvcGlobalAuthorize.Controllers;
namespace MvcGlobalAuthorize.Filters {
public sealed class LogonAuthorize : AuthorizeAttribute {
public override void OnAuthorization(AuthorizationContext filterContext) {
bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)
|| filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
if (!skipAuthorization) {
base.OnAuthorization(filterContext);
}
}
}
}