Web.config:
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="1" />
</authentication>
<sessionState mode="InProc" cookieless="false" timeout="1"></sessionState>
</system.web>
注册:
FormsAuthentication.SetAuthCookie(model.UserName, false);
在我的登录方法中,我还设置了一个身份验证cookie:
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
记住我总是假的。
在我的导航栏中,我有一个条件,用于检查请求是否经过身份验证,如果不是,则显示登录按钮。
@if(Request.IsAuthenticated)
{....}
else
{
<ul class="nav nav-pills">
<li role="presentation">@Html.ActionLink("Home", "Index", "Home", null, new { title = "Return to the homepage" })</li>
<li role="presentation">@Html.ActionLink("Log On", "LogOn", "Account", null, new { title = "To Logon to the site." })</li>
</ul>
}
一分钟后,我很确定用户已注销,因为 Nav 显示登录按钮,而不是注销按钮,但是当我单击链接时,它会将我带到该页面而不是重定向到登录页面。
这可能是因为我没有将 [Authorize] 属性添加到我的控制器方法吗? 我是 .NET MVC 开发的初学者,因此非常感谢任何帮助。
是的
,要使站点知道您希望用户登录,您必须将[Authorize]
装饰器添加到控制器或单个操作中。