Nee*_*eta 0 asp.net-mvc authorization
我正在使用ASP.NET授权在登录前拒绝用户访问我的网站,但这也阻止了Register.cshtml页面.如何整理授权以允许此页面通过?
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
<location path="Content">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="Register.cshtml">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
恕我直言,您不应该使用web.config来控制应用程序的身份验证而是使用Authorize属性.
Global.asax在RegisterGlobalFilters方法下将其添加到您的文件中
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AuthorizeAttribute());
}
Run Code Online (Sandbox Code Playgroud)
或者你也可以装饰你的控制器 [Authorize]
[Authorize]
public class HomeController : Controller
{
...
}
Run Code Online (Sandbox Code Playgroud)
对于需要匿名访问使用AllowAnonymous属性的操作
[AllowAnonymous]
public ActionResult Register() {
// This action can be accessed by unauthorized users
return View("Register");
}
Run Code Online (Sandbox Code Playgroud)
根据参考,
您不能使用路由或web.config文件来保护您的MVC应用程序.保护MVC应用程序唯一受支持的方法是将Authorize属性应用于每个控制器,并在登录和注册操作上使用新的AllowAnonymous属性.根据当前区域做出安全决策是非常糟糕的事情,并会将您的应用程序打开到漏洞中.
| 归档时间: |
|
| 查看次数: |
4766 次 |
| 最近记录: |