0

如果用户当前已登录,我想呈现注销按钮。

我想把按钮放在 _Layout.cshtml 页面中。

如何检查用户是否已登录?

4

2 回答 2

2
@if(Request.IsAuthenticated) {
    @Html.ActionLink("Your text goes here.", "LogOff", "YourControllerName", null, null)
}
于 2012-10-04T15:27:08.143 回答
1
If you are using Form Authentication , try 

// Inside Controller

public ActionResult LogOff()
{
    FormsAuthentication.SignOut();
    return RedirectToAction("Index", "Home");
}

// Inside View (Layout Page) Razor Engine

@if(User.Identity.IsAuthenticated){

  @Html.ActionLink("Your text goes here.", "LogOff", "YourControllerName")

}
于 2012-10-04T15:38:56.593 回答