FormsAuthentication
-
Hi to all, I am trying to use FormsAuthentication in my Project but I am getting some Problems. Code 1: Private Sub imgbtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnSubmit.Click Try 'call the ValidateLogin function If ValidateUser(txtLoginId.Value.ToUpper, txtPassword.Value.ToUpper) Then If Request.Params("ReturnUrl") <> "" Then FormsAuthentication.RedirectFromLoginPage(txtLoginId.Value, True) Else If iStatus = 1 Then FormsAuthentication.SetAuthCookie(txtLoginId.Value, True) Server.Transfer("EntrySubscriberDtls.aspx") Else FormsAuthentication.SetAuthCookie(txtLoginId.Value, True) Server.Transfer("ViewSubscriberDtls.aspx") End If End If Else Response.Redirect("VendorLogin.aspx", True) End If Catch ex As Exception 'Throw error Message txtLoginId.Value = "" txtPassword.Value = "" lblErrorMsg.Text = "Invalid Username or Password" End Try End Sub Code 2: Function ValidateUser(ByVal UserName As String, ByVal Pssword As String) As Boolean Dim vsVndrPwd As String Dim dtVendorDet As New DataTable Try 'For cheking Valid UserName and Password Session("Username") = userName vsVndrPwd = Nothing dtVendorDet = Objallschoolinfo.ValidateLogin(txtLoginId.Value.ToUpper.Trim) iStatus = Convert.ToInt32(dtVendorDet.Rows(0).Item("bStatus")) vsVndrPwd = dtVendorDet.Rows(0).Item("vsPassword".ToUpper.Trim) Catch ex As Exception lblErrorMsg.Text = ex.Message End Try 'If Not Found the Record If (vsVndrPwd Is Nothing) Then Return False End If 'if Found the Record Return (String.Compare(vsVndrPwd.ToUpper.Trim, Pssword.ToUpper.Trim, False) = 0) End Function Code:3 Web.config file ----------------- Error: When iStatus=1 It's th
-
Hi to all, I am trying to use FormsAuthentication in my Project but I am getting some Problems. Code 1: Private Sub imgbtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnSubmit.Click Try 'call the ValidateLogin function If ValidateUser(txtLoginId.Value.ToUpper, txtPassword.Value.ToUpper) Then If Request.Params("ReturnUrl") <> "" Then FormsAuthentication.RedirectFromLoginPage(txtLoginId.Value, True) Else If iStatus = 1 Then FormsAuthentication.SetAuthCookie(txtLoginId.Value, True) Server.Transfer("EntrySubscriberDtls.aspx") Else FormsAuthentication.SetAuthCookie(txtLoginId.Value, True) Server.Transfer("ViewSubscriberDtls.aspx") End If End If Else Response.Redirect("VendorLogin.aspx", True) End If Catch ex As Exception 'Throw error Message txtLoginId.Value = "" txtPassword.Value = "" lblErrorMsg.Text = "Invalid Username or Password" End Try End Sub Code 2: Function ValidateUser(ByVal UserName As String, ByVal Pssword As String) As Boolean Dim vsVndrPwd As String Dim dtVendorDet As New DataTable Try 'For cheking Valid UserName and Password Session("Username") = userName vsVndrPwd = Nothing dtVendorDet = Objallschoolinfo.ValidateLogin(txtLoginId.Value.ToUpper.Trim) iStatus = Convert.ToInt32(dtVendorDet.Rows(0).Item("bStatus")) vsVndrPwd = dtVendorDet.Rows(0).Item("vsPassword".ToUpper.Trim) Catch ex As Exception lblErrorMsg.Text = ex.Message End Try 'If Not Found the Record If (vsVndrPwd Is Nothing) Then Return False End If 'if Found the Record Return (String.Compare(vsVndrPwd.ToUpper.Trim, Pssword.ToUpper.Trim, False) = 0) End Function Code:3 Web.config file ----------------- Error: When iStatus=1 It's th
hi! your problem is the Server.Transfer method in both cases. you have to do a Response.Redirect(...) with the client so that he gets the auth-cookie. have a look at the differences of both methods in your VS-help. Make sure that you catch ThreabAbortException which is thrown by Response.Redirect. in your case: Private Sub imgbtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnSubmit.Click Try ... Catch exc as ThreadAbortException ' Do nothing here Catch ex As Exception 'Throw error Message txtLoginId.Value = "" txtPassword.Value = "" lblErrorMsg.Text = "Invalid Username or Password" End Try End Sub -walter
-
hi! your problem is the Server.Transfer method in both cases. you have to do a Response.Redirect(...) with the client so that he gets the auth-cookie. have a look at the differences of both methods in your VS-help. Make sure that you catch ThreabAbortException which is thrown by Response.Redirect. in your case: Private Sub imgbtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnSubmit.Click Try ... Catch exc as ThreadAbortException ' Do nothing here Catch ex As Exception 'Throw error Message txtLoginId.Value = "" txtPassword.Value = "" lblErrorMsg.Text = "Invalid Username or Password" End Try End Sub -walter
Thanks for you reply. Now, I change the Code: Try 'call the ValidateLogin function If ValidateUser(txtLoginId.Value.ToUpper, txtPassword.Value.ToUpper) Then If Request.Params("ReturnUrl") <> "" Then FormsAuthentication.RedirectFromLoginPage(txtLoginId.Value, True) Else If iStatus = 1 Then Response.Redirect("EntrySubscriberDtls.aspx") Else Response.Redirect("ViewSubscriberDtls.aspx") End If End If Else Response.Redirect("VendorLogin.aspx", True) lblErrorMsg.Text = "Invalid UserName or Password" End If Catch ex As Exception 'Throw error Message lblErrorMsg.Text = ex.Message End Try -------------------------- In Both Case: Error is:"Thread was being aborted." I am Using Backend code VB.NET and ASP.NET 1.1 Mohan Balal
-
Thanks for you reply. Now, I change the Code: Try 'call the ValidateLogin function If ValidateUser(txtLoginId.Value.ToUpper, txtPassword.Value.ToUpper) Then If Request.Params("ReturnUrl") <> "" Then FormsAuthentication.RedirectFromLoginPage(txtLoginId.Value, True) Else If iStatus = 1 Then Response.Redirect("EntrySubscriberDtls.aspx") Else Response.Redirect("ViewSubscriberDtls.aspx") End If End If Else Response.Redirect("VendorLogin.aspx", True) lblErrorMsg.Text = "Invalid UserName or Password" End If Catch ex As Exception 'Throw error Message lblErrorMsg.Text = ex.Message End Try -------------------------- In Both Case: Error is:"Thread was being aborted." I am Using Backend code VB.NET and ASP.NET 1.1 Mohan Balal
As I said before, you have to catch System.Threading.ThreadAbortException. ThreadAbortException is thrown exactly after you do a Response.Redirect. This is normal and it's because the .NET-Framework informs itself that the current thread is beeing aborted. Your problem was that you generally catch every type of exception. Instead you must catch the ThreadAbortException BEFORE you catch any other Exception types. Before you go on, take a look on the documentation of "System.Threading.ThreadAbortException", afterwards it will be clear why you get the message "Thread was being aborted." so change your code to this: Try 'call the ValidateLogin function If ValidateUser(txtLoginId.Value.ToUpper, txtPassword.Value.ToUpper) Then If Request.Params("ReturnUrl") <> "" Then FormsAuthentication.RedirectFromLoginPage(txtLoginId.Value, True) Else If iStatus = 1 Then Response.Redirect("EntrySubscriberDtls.aspx") Else Response.Redirect("ViewSubscriberDtls.aspx") End If End If Else Response.Redirect("VendorLogin.aspx", True) lblErrorMsg.Text = "Invalid UserName or Password" End If Catch ex as System.Threading.ThreadAbortException ' Do nothing here, just catch the exception Catch ex As Exception 'Throw error Message lblErrorMsg.Text = ex.Message End Try hope that helps - walter