Forms Authentication Problem...

G

Guest

Can someone please tell me why I keep being redirected back to the login page
even though I have logged in with the correct cridentials!!!

I have used this code before on another site and it works fine but when I
copied it across to the new site it stops working! All the variables are
passed when I do a debug but it just returns me to the login page again and
again!

Please help! It's driving me nuts!

Thanks!!!

<<CODE>>>

If Page.IsValid Then
'Connect to Database for User Validation
If DBConnection(txtUserName.Text, txtPassword.Text) Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,
Remember.Checked)
Else
' Credentials are Invalid
lblMessage.Text = "Invalid Login!"
End If

End If
End Sub


Function DBConnection(ByVal txtUserName As String, ByVal txtPassword As
String) As Boolean


Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("ValidateUser", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Dim objParam1, objParam2 As SqlParameter

objParam1 = cmd.Parameters.Add("@UserName", SqlDbType.Char)
objParam2 = cmd.Parameters.Add("@Password", SqlDbType.Char)

objParam1.Direction = ParameterDirection.Input
objParam2.Direction = ParameterDirection.Input

objParam1.Value = txtUserName
objParam2.Value = txtPassword

Try
If Myconn.State = ConnectionState.Closed Then
Myconn.Open()
End If

Dim objReader As SqlDataReader
objReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

While objReader.Read()
If CStr(objReader.GetValue(0)) <> "1" Then
lblMessage.Text = "Invalid Login!"
Else
objReader.Close()
Return True
End If

End While
Catch ex As Exception
lblMessage.Text = "Error Connecting to Database!"
End Try

End Function

<<<Web.Config>>>

<authentication mode="Forms">
<forms name="NWLogin" loginUrl="Admin/Login.aspx" />
</authentication>
<!-- AUTHORIZATION
This section sets the authorization policies of the application.
You can allow or deny access
to application resources by user or role. Wildcards: "*" mean
everyone, "?" means anonymous
(unauthenticated) users.
-->
<authorization>

<allow roles="Administrator, User" /> <!-- ONLY USERS ALLOWED THAT BELONG
TO THESE GROUPS -->
<deny users="*" /> <!-- Deny all users -->

<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>
 
P

Patrick.O.Ige

So what were you doing wrong?
Patrick:)
Tim::.. said:
Please forget the last mail!

I managed to figure it out!
Thanks

Tim::.. said:
Can someone please tell me why I keep being redirected back to the login page
even though I have logged in with the correct cridentials!!!

I have used this code before on another site and it works fine but when I
copied it across to the new site it stops working! All the variables are
passed when I do a debug but it just returns me to the login page again and
again!

Please help! It's driving me nuts!

Thanks!!!

<<CODE>>>

If Page.IsValid Then
'Connect to Database for User Validation
If DBConnection(txtUserName.Text, txtPassword.Text) Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,
Remember.Checked)
Else
' Credentials are Invalid
lblMessage.Text = "Invalid Login!"
End If

End If
End Sub


Function DBConnection(ByVal txtUserName As String, ByVal txtPassword As
String) As Boolean


Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("ValidateUser", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Dim objParam1, objParam2 As SqlParameter

objParam1 = cmd.Parameters.Add("@UserName", SqlDbType.Char)
objParam2 = cmd.Parameters.Add("@Password", SqlDbType.Char)

objParam1.Direction = ParameterDirection.Input
objParam2.Direction = ParameterDirection.Input

objParam1.Value = txtUserName
objParam2.Value = txtPassword

Try
If Myconn.State = ConnectionState.Closed Then
Myconn.Open()
End If

Dim objReader As SqlDataReader
objReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

While objReader.Read()
If CStr(objReader.GetValue(0)) <> "1" Then
lblMessage.Text = "Invalid Login!"
Else
objReader.Close()
Return True
End If

End While
Catch ex As Exception
lblMessage.Text = "Error Connecting to Database!"
End Try

End Function

<<<Web.Config>>>

<authentication mode="Forms">
<forms name="NWLogin" loginUrl="Admin/Login.aspx" />
</authentication>
<!-- AUTHORIZATION
This section sets the authorization policies of the application.
You can allow or deny access
to application resources by user or role. Wildcards: "*" mean
everyone, "?" means anonymous
(unauthenticated) users.
-->
<authorization>

<allow roles="Administrator, User" /> <!-- ONLY USERS ALLOWED THAT BELONG
TO THESE GROUPS -->
<deny users="*" /> <!-- Deny all users -->

<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top