Introduction:
Asp.Net Security Model have various types of aurhentication Methods.In this,article we are going to eloborate common authendication method FormsAuthendication.
Main:
Forms authentication supports the following Methods,
|
FormsAuthentication |
Method Description |
|---|---|
|
CookiesSupported |
Property indicating whether cookies are supported for authentication |
|
FormsCookieName |
Property representing the forms authentication cookie name |
|
FormsCookiePath |
Property representing the forms authentication cookie path |
|
LoginUrl |
Redirects URL for logging in |
|
RequireSSL |
Property representing whether secure sockets layer is required |
|
SlidingExpiration |
Property indicating whether sliding expiration is set |
|
Authenticate |
Authenticates the user |
|
Encrypt |
Generates an encrypted string representing a forms-authentication ticket suitable for use in an HTTP cookie |
|
Decrypt |
Creates a FormsAuthenticationTicket from an encrypted forms-authentication ticket |
|
GetAuthCookie |
Creates an authentication cookie for a specif c user |
|
GetRedirectUrl |
Gets the original URL to which the client was surfing |
|
HashPasswordForStoringInConfigFile |
Creates a hashed password suitable for storing in a credential store |
|
RedirectFromLoginPage |
Authenticates the user and redirects to the originally requested page |
|
SignOut |
Invalidates the authentication ticket |
Step-1:Declare authentication method in app.config,
Force Authentication:
<configuration>
<system.web>
<authentication mode=”Forms”>
<forms loginUrl=”login.aspx” />
</authentication>
<authorization>
<deny users=”?” />
</authorization>
</system.web>
</configuration>
Optional:
<configuration>
<system.web>
<authentication mode=”Forms”>
</authentication>
</system.web>
</configuration>
Step-2:Create login page with available login controls,
protected bool ISAuthenticateUser(String strUserName,
String strPassword)
{
if (strUserName == “Johny”)
{
if(strPassword== “*******”)
{
return true;
}
}
return false;
}
public void OnLogin(Object src, EventArgs e)
{
if (ISAuthenticateUser(m_textboxUserName.Text,
m_textboxPassword.Text))
{
FormsAuthentication.RedirectFromLoginPage(
m_textboxUserName.Text, m_bPersistCookie.Checked);
} else {
Response.Write(“Invalid login: You don’t belong here…”);
}
}
In aspx,
<asp:TextBox id=”m_textboxUserName” runat=server/><br>
Password:
<asp:TextBox id=”m_textboxPassword”
TextMode=”password” runat=server/>
<br/>
Remember password:
<asp:CheckBox id=m_bPersistCookie runat=”server”/>
<br/>
<asp:Button text=”Login” OnClick=”OnLogin”
runat=server/>
Conclusion:
Hope,this helps,
Happy Coding.