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
如果您希望我发布更多的代码或信息,请让我知道。