2011-03-15 44 views
0

動態登錄按鈕我有這樣的代碼:我將如何創建MVC3

<!DOCTYPE html> 
<html> 
<head> 
    <title>@ViewBag.Title</title> 
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script> 
</head> 

<body> 
<div id="header"> 
    <div id="header-inside"> 
     <div id="logo"></div> 
     <div id="top-menu"> 
      <a href="#">Home</a> | <a href="#">About</a> | <a href="#">Contact</a> | 
     </div> 
    </div> 
</div> 
    <div id="content"> 
     @RenderBody() 
    </div> 
</body> 
</html> 

我想添加一個登錄按鈕那裏,將檢測用戶是否登錄,如果用戶是它會變成「註銷」。我使用會員服務,所以我會用的MembershipUser

回答

3

如果您有Request對象,你可以使用Request.IsAuthenticated(MSDN文檔here)不管你如何管理你的用戶在幕後,只要插在其中將工作系統ASP .NET提供。

例:

@if(Request.IsAuthenticated){ 
    <div id="logout_button"/> 
} else { 
    <div id="login_button"/> 
} 
1

您可以使用Request.IsAuthenticated來檢測用戶是否登錄

0
<%= Page.User.Identity.IsAuthenticated %> 

<% Request.IsAuthenticated %> should do the trick 
+1

您的代碼使用較舊的ASP .NET WebForms標記。在ASP .NET MVC 3中,默認引擎是使用Razor表達式語言的Razor ViewEngine。 http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx他的例子也使用它。 – 2011-03-15 06:42:05

+0

你是對的,沒有看到MVC3的標題。投票評論你的評論... – timothyclifford 2011-03-16 03:32:04

0

下面是基於_LogOnPartial.cshtml從一個片段默認項目模板:

@if(Request.IsAuthenticated) {   
    using (Html.BeginForm("LogOff", "Account")) 
    { 
     <input type="submit" name="login" value="Log Off" /> 
    } 
} 
else 
{ 
    @:[ @Html.ActionLink("Log On", "LogOn", "Account") ] 
}