2010-08-09 95 views
1

我在我的VS-2005網站中使用了表單身份驗證。ASP.NET - FormsAuthentication - 登錄後無法重定向

如果證書不正確或明確請求受保護的頁面,網站可以將用戶重定向到登錄頁面。但是,如果提供了正確的登錄憑據,則應用程序無法將用戶重定向到所需的頁面。

在調試過程中,我發現'Request.IsAuthenticated = False'就在我將用戶重定向到所需的頁面之前。編碼時,我認爲這個屬性在我生成認證票據後會自動設置爲true。那麼在驗證後,我需要在提交按鈕裏面明確地設置它嗎?

順便說一句我沒有使用'GetAuthcookie','SetAuthCookie'或'RedirectFromLoginPage'方法。 我在提交按鈕中單擊登錄頁面以及web.config中的身份驗證和授權標籤。

<authentication mode="Forms"> 
    <forms name=".ASPXFORMSDEMO" loginUrl="~/Login.aspx" cookieless="UseCookies" path="~/"/> 
</authentication> 
<authorization> 
    <deny users="?"/> 
</authorization> 

Protected Sub btnsubmit_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click 
    'here first validate if the user is valid user 
    ad = New Aranya_Data 
    Dim code As Integer = ad.validateuser(txtuserid.Text, txtpwd.Text) 

    'need to implement forms authentication here 
    If code = 0 Then 
     'creating the authentication ticket 

     Dim tkt As FormsAuthenticationTicket 
     Dim cookiestr As String = "" 
     Dim ck As HttpCookie 
     tkt = New FormsAuthenticationTicket(1, txtuserid.Text, DateTime.Now, DateTime.Now.AddMinutes(30), chkRemember.Checked, "14062010") 
     cookiestr = FormsAuthentication.Encrypt(tkt) 
     ck = New HttpCookie(FormsAuthentication.FormsCookieName, cookiestr) 
     If chkRemember.Checked Then 
      ck.Expires = tkt.Expiration 
     End If 
     ck.Path = FormsAuthentication.FormsCookiePath 
     Response.Cookies.Add(ck) 
     Dim strRedirect As String = "" 
     strRedirect = Request("ReturnUrl") 
     If strRedirect Is Nothing Then 
      strRedirect = "~/Second.aspx" 
     End If 
     Response.Redirect(strRedirect & "?usr=" & tkt.Name, True) 
    Else 
     MsgBox("Invalid Login credentials! Please try again.", MsgBoxStyle.OkOnly, "Please Note") 
    End If 

End Sub 

如果您希望我發佈更多的代碼或信息,請讓我知道。

回答

1

我設法找到一個解決方案,現在它的工作。

在'認證'標籤裏面刪除了web.config中'forms'標籤的'path'屬性,這就解決了這個問題。現在我的認證標籤如下所示: -

<authentication mode="Forms"> 
    <forms name=".ASPXFORMSDEMO" loginUrl="~/Login.aspx" cookieless="UseCookies" /> 
</authentication> 

感謝