Intorduction
Login Interface is most useful for knowing that who is accessing your website and protecting to unauthorized comments and access. In asp.net to implement login interface is quit easy. Platform needed: Visual Studio 12
Description
There are some steps to implements:
Step 1:
First of create an login form as given by drag and drop property of visual Studio 12
<form method=”get” action=”validate.aspx” >
<input id=”Text1″ type=”text” name=”username” style=”border-color:#0094ff;border-width:1px;border-radius:2px;margin-left:30px;color:#0094ff;opacity:0.7;” class=”auto-style2″ placeholder=”Email/Mobile” title=”Enter Email Address / Mobile Number” onfocus=”javascript:if(this.value==’Email/Mobile’){
this.value=”}” onblur=”javascript:if(this.value==”){
this.value=’Email/Mobile’}” />
<br /><br />
<input id=”Password1″ type=”password” name=”password” placeholder=”Password” style=”border-radius:2px;border-color:#0094ff;border-width:1px;margin-left:30px;color:#0094ff;” title=”Enter Password” class=”auto-style1″ onfocus=”javascript:if(this.value==’password’){
this.value=”}” onblur=”javascript:if(this.value==”){
this.value=’password’}” />
<br />
<a href=”forgot.aspx” style=”text-decoration:none;color:#0094ff;margin-left:30px;” title=”Click if you Forgot Password”>Forgot Password</a>
<input id=”Submit1″ type=”submit” value=”Signin” style=”margin-left:220px;color:#fff;background-color:#0094ff;height:30px;width:80px;” title=”Signin Now” />
</form>
Step 2:
Create a Login validator file validate.aspx and edit validate.aspx.cs file as given below. You need to create session for staying user on the webpage. Cookies will hold you login details.
String username, password;
username = Request.QueryString["username"].ToString();
password = Request.QueryString["password"].ToString();
if (login(username, password))
{
Session["username"] = username;
Session["name"] = name;
HttpCookie registration = new HttpCookie(“username”);
HttpCookie registration1 = new HttpCookie(“password”);
registration.Expires.AddMonths(2);
registration1.Expires.AddMonths(2);
Response.Redirect(“search.aspx”);
}
else
{
Response.Redirect(“login.aspx”);
}
step 3:
To verify email address create verify.aspx.cs as:
if (Session["email"] == null)
{
Response.Redirect(“verify1.aspx”);
}
else {
email=Session["email"].ToString();
if (TextBox2.Text.Length >= 5)
{
if (TextBox2.Text.Equals(TextBox3.Text))
{
set_password(email, TextBox3.Text, TextBox1.Text);
}
else
{
Label1.Text = “Password not Matched..”;
TextBox2.Text = “”;
TextBox3.Text = “”;
}}
else
{
Label1.Text = “Password must be greater than 4 character..”;
}}}
step 4:
To verify Mobile Number create verify1.aspx.cs file
String email = “”;
if (Session["email"] != null)
{
Response.Redirect(“verify.aspx”);
}
else
{
email = TextBox4.Text;
if (TextBox2.Text.Length >= 5)
{
if (TextBox2.Text.Equals(TextBox3.Text))
{
set_password(email, TextBox3.Text, TextBox1.Text);
}
else
{
Label1.Text = “Password not Matched..”;
TextBox2.Text = “”;
TextBox3.Text = “”;
}
}
else
{
Label1.Text = “Password must be greater than 4 character..”;
}}}
Step 5:
If user forgot password then create password reset link by creating forgot.aspx.cs file. First of all check whether user exist or not. If user is not registered then need to register. Md5 encryption is needed for securing the http data, while you are creating the password reset link.
String source = @”Data Source=PROT\SQLEXPRESS;Initial Catalog=cms;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False”;
protected void Page_Load(object sender, EventArgs e)
{
}
Boolean validate_email(String email)
{
if (email.Contains(‘@’) && email.Contains(‘.’))
{
return true;
}
else
return false;
}
Boolean mailSender(String code, String taget_email)
{
try
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(taget_email);
message.From = new System.Net.Mail.MailAddress(“inform@only4student.in”);
message.Subject = “Password Recovery mail for ISS”;
message.Body = “As you requested for recovery of password .<br/> Password recovery code : <b> ” + code + “</b> <br/> Go on following link to reset your Password : <br/>Thnak you”;
System.Net.Mail.SmtpClient smtp =new System.Net.Mail.SmtpClient();
smtp.Port = 25;
smtp.Host = “mail.only4student.in”;
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(“email@mail.in”, “*****”);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
return true;
}
catch (Exception ex)
{
Label2.Text = “We are not able to Change your password due to Network Problem.”+ex.Message;
//Error.Visible = true;
//Error.Text = “Recovery code IS : ” + code + “\n Plz. Copy it for further use and click on Hyperlink “;
//HyperLink1.Visible = true;
return false;
}
}
Boolean email_check(String email) {
String code = Membership.GeneratePassword(10, 5);
string select = “SELECT * FROM freeuser where email=’” + email + “‘”;
String query = “UPDATE freeuser SET recovercode=’” + code + “‘ where email=’” + email + “‘”;
SqlConnection conn = new SqlConnection(source);
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
object o = cmd.ExecuteScalar();
if (o == null)
{
Label2.Text = “Your Email is not Registered….<a href=’Default.aspx’>Register Today</a>”;
return false;
}
else
{
//conn.Open();
SqlCommand cmd1 = new SqlCommand(query, conn);
int rowsReturned = cmd1.ExecuteNonQuery();
conn.Close();
Session["email"] = email;
if (mailSender(code, email))
return true;
else
return false;
}}
protected void Button1_Click(object sender, EventArgs e)
{
String email=”";
email = TextBox1.Text;
if (!email.Equals(“”))
{
if (validate_email(email))
{
if (email_check(email))
{
Label2.Text = “Your Password verification code is sent to your email address….<a href=’verify.aspx’>Verify Now</a>’”;
}
else
{
}
}
else {
Label2.Text = “Not a Valid Email Address…”;
}
}
else {
Label2.Text = “Please Specify you email Address..”;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect(“login.aspx”);
}
}
Time Required:
It will take 30-40 minutes to implement.