Click here to Skip to main content
Click here to Skip to main content

Using Form Authentication in your Application

By , 14 Oct 2010
 

Introduction

This is a complete Small Application that used Form Authentication Mode. In this i use one Login page. That is used for if User is not authorized the it will go for authorization Check. Login page will be appear. Here he/she type user name and password. if the are authrized they will sent back to requested page other wise they not get the permission to open that Requested page.

First We change the authentication Mode in Web.config the systax is

<authentication mode="Forms"> 

<forms name=".NavinsForm" loginUrl="logon.aspx" protection="All" path="/" timeout="30"/>

</authentication>

 

Now we change the authorization tag

<authorization>



<deny users="?"/>

<allow users="*" /> 

 </authorization> 

After Perform These Changes in Web.config. We write the code for Check authorized user and send back to Requested Page. First of all we Validate user. Create Function on Login.aspx.

Function ValidateUser(ByVal userName As String, ByVal passWord As String) As Boolean

Dim conn As SqlConnection

Dim cmd As SqlCommand

Dim lookupPassword As String

Session("Username") = userName & " Hello"

lookupPassword = Nothing

'CHECK FOR A INVALID USERNAME

If ((userName Is Nothing)) Then

System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName Failed.")

Return False

End If

'Check for invalid password

If (passWord Is Nothing) Then

System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.")

Return False

End If

If ((passWord.Length = 0) Or (passWord.Length > 25)) Then

System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of password failde.")

Return False

End If

Try

conn = New SqlConnection("Server=svr;Database=pubs;uid=sa")

conn.Open()

cmd = New SqlCommand("Select pwd from Users where uname=@userName", conn)

cmd.Parameters.Add("@userName", SqlDbType.VarChar, 25)

cmd.Parameters("@userName").Value = userName

lookupPassword = cmd.ExecuteScalar

cmd.Dispose()

conn.Dispose()

Catch ex As Exception

System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception" & ex.Message)

End Try

'if no password found

If (lookupPassword Is Nothing) Then

Return False

End If

Return (String.Compare(lookupPassword, passWord, False) = 0)

End Function

This Function is Check Either User is valid or not if user is not valid he will not get back to Requested Page. For Sent back to Requested Page Write This on Login_Button Click

Private Sub cmdLogin_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLogin.ServerClick

If ValidateUser(txtUserName.Value, txtUserPass.Value) Then

If Request.Params("ReturnUrl") <> "" Then

FormsAuthentication.RedirectFromLoginPage(txtUserName.Value, chkPersistCookie.Checked)

Else

FormsAuthentication.SetAuthCookie(txtUserName.Value, chkPersistCookie.Checked)

Server.Transfer("NewPage.aspx")

End If

Else

Response.Redirect("logon.aspx", True)

End If

End Sub

Here One Importeant thing is that if user directly call the login page and if he/she is authorized the he will go to the Default Page. By Default the name of Default page is Default.aspx. if this page is not in your application this will give Error. So, solution of this problem is Check the Querystring by

Request.Params("ReturnUrl")<>""

if user Directly open the login.aspx page he will goes to your Default page that you set. This is done by

FormsAuthentication.SetAuthCookie(txtUserName.Value, chkPersistCookie.Checked)

Server.Transfer("NewPage.aspx")

Here Default page is not Default.aspx but it is "NewPage.aspx". If user Request the other page of Application the he will goes to that Requested Page After Sucessfull login. This is Done By.

FormsAuthentication.RedirectFromLoginPage(txtUserName.Value, chkPersistCookie.Checked)

This is All About Form Authentication. The Database Structure is look like this

CREATE TABLE [dbo].[Users] (
 [uname] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
 [Pwd] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
 [userRole] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) ON [PRIMARY]
GO

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Navin Singhwane
Web Developer
India India
Member
~ns

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGFYmemberVladimir_V7 Jul '06 - 16:09 
see above
GeneralRe: GFYmemberNavin Singhwane10 Jul '06 - 23:11 
What
 
Navi
GeneralRe: GFYmembervedcyrus1 Aug '06 - 23:55 
Hi Navin I have tried your code but it is not working.
I takes me to the login page all the time even if the user is authorised.
No other page is displayed except the loginpage or the page we specify in web.config file
 
please help me out in this reagard
 
thanx in advance
 
reply at vedcyrus@yahoo.com
 
chandan
 

GeneralRe: GFYmemberNavin Singhwane5 Aug '06 - 0:29 
hi write this code in ur login button
If ValidateUser(txtUserName.Value, txtUserPass.Value) Then If Request.Params("ReturnUrl") <> "" Then FormsAuthentication.RedirectFromLoginPage(txtUserName.Value, chkPersistCookie.Checked)Else
FormsAuthentication.SetAuthCookie(txtUserName.Value, chkPersistCookie.Checked)
Server.Transfer("NewPage.aspx")
End If
Else
Response.Redirect("logon.aspx", True)
End If
 
Navi

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 14 Oct 2010
Article Copyright 2006 by Navin Singhwane
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid