2011-07-20 60 views
0

我正在登錄/註冊模塊(FYI no rolls/membership只是一個簡單的登錄表單),數據流來自sql server,用於註冊和登錄,代碼是目前工作正常。 我想在我的代碼中使用登錄/註銷功能。我已經創建了一個登錄表單,用戶可以登錄,登錄成功後,用戶被重定向到default_page.aspx。 我[R & d碼高達這是因爲:關於登錄/註銷,在asp.net中進行身份驗證

<td width="122" align="right"><asp:LinkButton ID="LinkButton1" runat="server" 
      onclick="LinkButton1_Click">Sign Out</asp:LinkButton></td> 

CS頁:

protected void LinkButton1_Click(object sender, EventArgs e) 
{ 
    //After click Log out we need to delete all session values 


    Session.RemoveAll(); 
    Response.Redirect("userlogin.aspx"); 
} 

從上面的代碼中可以看出U我已經把只有一個LinkBut​​ton,如果用戶點擊上面的鏈接按鈕,用戶得到了註銷並重定向到loginpage(這裏是userlogin.aspx)。但是現在如果我正在嘗試更改(請參閱下面的aspx和.cs)signin | signout,我得到的錯誤如下:'System.Security.Principal.IPrincipal'不包含'IsAuthenticated'的定義,也沒有擴展方法' IsAuthenticated」接受一個類型的第一個參數 'System.Security.Principal.IPrincipal' 可以找到(是否缺少using指令或程序集引用?)

aspx頁面:

<td width="122" align="right"><asp:LinkButton ID="LinkButtonSignInOut" runat="server" 
      onclick="LinkButton1_Click">""</asp:LinkButton></td> 

的.cs頁碼:

protected void LinkButton1_Click(object sender, EventArgs e) 
{ 
    //After click Log out we need to delete all session values 
    if (User.IsAuthenticated)**//error is here** 
     LinkButtonSignInOut.Text = "Sign Out"; 
    else 
     LabelSignInOut.Text = "Sign In"; 

    Session.RemoveAll(); 
    Response.Redirect("userlogin.aspx"); 
} 

任何建議???

謝謝!

回答

0

檢查下面的代碼,我已經寫了評論

protected void LinkButton1_Click(object sender, EventArgs e) 
{ 
    if (User.IsAuthenticated) **//error is here** 
    if(User.Identity.IsAuthenticated) // this is correct code 
     LinkButtonSignInOut.Text = "Sign Out"; 
    else 
     LabelSignInOut.Text = "Sign In"; 

    Session.RemoveAll(); // 
    Session.Abandon(); // Better to use this 
    Response.Redirect("userlogin.aspx"); 
    // You are display assigning text in label and immediate after you are redirecting it to another page ?? 
} 
+0

@thanks ......但目前沒有窗戶,檢查你的代碼...是你說得對,錯重定向...但目前我我正在爲此工作。 – Alok