加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 允许Anonymous在asp.net mvc 3中调用某些操作

发布时间:2020-12-16 04:01:38 所属栏目:asp.Net 来源:网络整理
导读:我有一个名为ForgetPassword的动作.每次匿名尝试检索操作时,他/她都会被重定向到登录页面.以下是我的实现. public ActionResult ForgotPassword(string UserName){ //More over when i place a breakpoint for the below line //its not even getting here r
我有一个名为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]
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属性应用于每个控制器,并在登录和注册操作上使用新的AllowAnonymous属性.根据当前区域做出安全决策是非常糟糕的事情,并会将您的应用程序打开到漏洞中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读