2012-03-09 45 views
0

这个帖子类似:MVC 3 - FormsAuthentication - Can't give access to my Login actionASP.NET MVC3 Forms身份验证不允许后登录行动

我无法让我的表单提交的用户名/密码来登录动作的处理后。但是,我的AccountController继承了默认控制器,即没有[Authorize]-attribute。我的帐户控制是这样的:

[HttpGet] 
public ActionResult LogOn() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult LogOn(LogOnModel model, string returnUrl = null) 
{ 
    ... 
} 

而且我的web.config中包含,除其他外:

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

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

    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/LogOn" timeout="2880" defaultUrl="~/Home/Index" /> 
    </authentication> 

在提交登录表单时,它会立即重定向我到[HttpGet]版本LogOn-的行动。我从未被定向到POST版本。我得到同样的反应在上面的文章中提到:

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

如果我注释掉<authorization>一切工作正常。但是,在这种情况下,当我输入一个需要授权的页面的深层链接时,我不会重定向到登录页面。

我也尝试添加位置“帐户”,并允许所有用户,但这似乎没有任何影响。

任何人都可以指出我做错了什么?

编辑

在菲德勒我总是看到这样的图案:

# Result Protocol Host URL Body Caching Content-Type Process Comments Custom 
1 302 HTTP localhost:36372/ 145 private  text/html; charset=utf-8 iexplore:6400   
2 200 HTTP localhost:36372 /Account/LogOn?ReturnUrl=%2f 4,752 private  text/html; charset=utf-8 iexplore:6400   

为什么是第一个URL总是/

EDIT2

它发生在我检查生成的HTML,让我吃惊,它看起来是这样的:

<form action="/" id="LogOnForm" method="post"> 

很显然,这不是正确的动作。 ASP代码看起来是这样的:

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "LogOnForm" })) 

我的路线是这样的:

routes.MapRoute(
    "Default", // Route name 
    "{controller}/{action}/{id}", // URL with parameters 
    new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults 
); 

回答

0

您需要添加位置标签用于/帐号/登录,并允许匿名访问。

+0

正如我在文章中提到的,我已经尝试过了,但它没有效果。 – Pieter 2012-03-09 10:12:20

+0

您可以粘贴您的帐户/登录位置标记以及来自web.config的formsAuthentication内容吗?网络。以上配置只有“帐户”的位置,您可以尝试一次将其更改为帐户/登录? – Sachin 2012-03-09 10:20:34

+0

我在阅读您的文章后尝试过,但没有效果。可能这也不是问题。查看我的编辑。 – Pieter 2012-03-09 10:24:30

1

您可以通过在MVC

[Authorize] 
    public ViewResult SubmitPost() 
    { 
     return View(); 
    }  

使用简单的授权属性设置授权,你必须设置cookie的授权,仅当浏览器关闭

FormsAuthentication.SetAuthCookie("Name", false); 
默认会创建加密的Cookie中&将 到期