asp.net授权 - 拒绝所有登录前,除了注册页面 [英] asp.net authorization - deny all before login except the register page

查看:146
本文介绍了asp.net授权 - 拒绝所有登录前,除了注册页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET授权拒绝用户登录前访问我的网站,但是这也阻挡Register.cshtml页面。
我如何理清我的授权,通过让这个页面?

 <&的System.Web GT;
<授权>
      <拒绝用户=? />
    < /授权>
  < /system.web>  <位置路径=内容>
    <&的System.Web GT;
      <授权>
        <让用户=*/>
      < /授权>
    < /system.web>
  < /地点>  <位置路径=Register.cshtml>
    <&的System.Web GT;
      <授权>
        <让用户=*/>
      < /授权>
    < /system.web>
  < /地点>


解决方案

恕我直言,你不应该使用的web.config控制应用程序的认证,而不是使用授权属性

的Global.asax 文件添加这个在 RegisterGlobalFilters

 公共静态无效RegisterGlobalFilters(GlobalFilterCollection过滤器)
    {
        filters.Add(新HandleErrorAttribute());
        filters.Add(新AuthorizeAttribute());
    }

或者你也可以装饰你的控制器 [授权]

  [授权]
公共类HomeController的:控制器
{
    ...
}

对于要求匿名访问使用行动使用AllowAnonymous 属性

  [使用AllowAnonymous]
   公众的ActionResult寄存器(){
      //这个动作可以被未经授权的用户访问
      返回视图(注册);
   }

作为每个<一href=\"http://blogs.msdn.com/b/rickandy/archive/2012/03/23/securing-your-asp-net-mvc-4-app-and-the-new-allowanonymous-attribute.aspx\"相对=nofollow>参考,


  

您不能使用的路由的或web.config文件中,以确保你的MVC应用程序。以确保您的MVC应用程序的唯一方式是应用授权属性每个控制器并使用新的使用AllowAnonymous属性上的登录和注册行为。使得基于当前地区安全的决定是一个非常糟糕的事情,并会打开你的应用程序中的漏洞。


I'm using ASP.NET Authorization to deny users access to my site before logging in, but this is also blocking the Register.cshtml page. How do I sort out my authorizations to allow this page through?

<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>

解决方案

IMHO, you should not use web.config to control the authentication of your application instead use Authorize attribute.

Add this in your Global.asax file under RegisterGlobalFilters method

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

or you can decorate also your controller with [Authorize]

[Authorize]
public class HomeController : Controller
{
    ...
}

For action which require Anonymous access use AllowAnonymous attribute

   [AllowAnonymous]
   public ActionResult Register() {
      // This action can be accessed by unauthorized users
      return View("Register");   
   }

As per Reference,

You cannot use routing or web.config files to secure your MVC application. The only supported way to secure your MVC application is to apply the Authorize attribute to each controller and use the new AllowAnonymous attribute on the login and register actions. Making security decisions based on the current area is a Very Bad Thing and will open your application to vulnerabilities.

这篇关于asp.net授权 - 拒绝所有登录前,除了注册页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆