gpt4 book ai didi

asp.net-mvc - 允许匿名调用 asp.net mvc 3 中的某些操作

转载 作者:行者123 更新时间:2023-12-02 08:08:15 25 4
gpt4 key购买 nike

我有一个名为 ForgetPassword 的操作。每次匿名尝试检索操作时,他/她都会被重定向到登录页面。以下是我的实现。

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>

最佳答案

因为您通过使用拒绝了每个人的申请。

<authorization>
<deny users="?"/>
</authorization>

恕我直言,您不应使用 web.config 来控制应用程序的身份验证,而应使用 Authorize 属性。

将此添加到 RegisterGlobalFilters 方法下的 Global.asax 文件中

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AuthorizeAttribute()); //Added
}

或者您也可以使用[Authorize]来装饰您的 Controller

[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");;
}

根据Reference ,您不能使用路由或 web.config 文件来保护您的 MVC 应用程序。保护 MVC 应用程序的唯一受支持的方法是将 Authorize 属性应用于每个 Controller ,并在登录和注册操作中使用新的 AllowAnonymous 属性。根据当前区域做出安全决策是一件非常糟糕的事情,并且会让您的应用程序容易受到攻击。

关于asp.net-mvc - 允许匿名调用 asp.net mvc 3 中的某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com