MVC 3 - FormsAuthentication - 无法访问我的登录操作 [英] MVC 3 - FormsAuthentication - Can't give access to my Login action

查看:290
本文介绍了MVC 3 - FormsAuthentication - 无法访问我的登录操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC 3和FormsAuthentication:

I'm using MVC 3 and FormsAuthentication:

<authentication mode="Forms">
  <forms loginUrl="~/Account/" timeout="2880" />      
</authentication>

我的帐户/索引页面显示登录页面,然后使用Jquery,它发布到帐户/登录以验证用户。这是我的AccountController操作处理它:

My Account/Index page displays a login page, then using Jquery, it does a post to Account/Login to authenticate the user. Here's my AccountController action which handles it:

[HttpPost]
[Authorize(Users = "*")]
public ActionResult Login(string userName, string password) {
    ...
}

和发布到它的Jquery:

And the Jquery which posts to it:

$.post("/account/login", {
                "userName": $("#userName").val(),
                "password": $("#password").val()
            }, function (data) {
...});

以上所有工作,但现在我想限制对网站的一般访问,到我的根web.config:

All the above works, but now I want to restrict general access to the site, so I added this to my root web.config:

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

为了允许访问帐户控制器,我添加了以下内容:

And in an attempt to allow access to the Account controller, I added this:

<location path="~/Account">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>
</location>

这是我的问题。我可以查看登录页面(帐户/索引),但我不能提交到帐户/登录。当我看到通过Fiddler发生的事情,我得到这个文本从服务器返回时发布到该页面:

Here's my problem. I can view the login page (Account/Index), but I can't submit to Account/Login. When I watch what's going on through Fiddler, I get this text returned from the server when posting to that page:

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/Account/?ReturnUrl=%2faccount%2flogin">here</a>.</h2>
</body></html>

似乎当我在完整的授权用户列表中添加〜/ Account真的工作像在一个非mvc项目。在MVC 3中有一个特殊的方法吗?

It seems that when I add "~/Account" to the full authorized user list it isn't really working like it would in a non-mvc project. Is there a special way to do this in MVC 3?

推荐答案

控制器的路径通常不被引用为相对路径中。您应该只能说location =Account,例如

The path to a controller is typically not referenced as a relative path. You should just be able to say location="Account", e.g.

<location path="Account">

无法确定是您的问题,因为我现在无法测试, be ...

Can't be sure that's your issue as I can't test it right now, but it could be...

这篇关于MVC 3 - FormsAuthentication - 无法访问我的登录操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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