Friday 3 August 2012

Login Example in asp.net using c#.



First You create a table name login.
Give the column nane user_name and password.

sql query:

create table login(user_name string,password string);

after  that open visual studio 2010. File>open new website>give name >ok.

default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         User Name:<asp:TextBox ID="TxtName" runat="server" ></asp:TextBox> 
        Password:<asp:TextBox ID="TxtPassword" runat="server" TextMode="Password"></asp:TextBox>                                           
    </div>
    </form>
</body>
</html>



default.cs







using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
protected void BtnLogin_Click(object sender, EventArgs e)
    {
                string connectionInfo = ConfigurationManager.AppSettings["ConnectionInfo"];
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = connectionInfo;
        if (cn.State == ConnectionState.Closed)
        {
            cn.Open();
        }
        SqlCommand _com = new SqlCommand("select * from registrations", cn);
        SqlParameter p1 = new SqlParameter("user_id", TxtLogin.Text);
        SqlParameter p2 = new SqlParameter("user_name", TxtPassword.Text);
        _com.Parameters.Add(p1);
        _com.Parameters.Add(p2);
         //cn.Open();
            SqlDataReader _dr = _com.ExecuteReader();
            if (_dr.HasRows)
            {
                while (_dr.Read())
                {
                    Response.Redirect("HomePage.aspx");\\ Home.aspx page will open when username and password will correct.
                }
            }
          
    }
}

change the web.config file  setting as below:
 see the highlited text in red color
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <appSettings>
        <add key="ConnectionInfo" value="Data Source =SUSHIL-70F1F267; Initial Catalog =sushil; Integrated Security=true"/>
    </appSettings>

    <connectionStrings>
        <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <authentication mode="Forms">
            <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
        </authentication>
        <membership>
            <providers>
                <clear/>
                <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
            </providers>
        </membership>
        <profile>
            <providers>
                <clear/>
                <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
            </providers>
        </profile>
        <roleManager enabled="false">
            <providers>
                <clear/>
                <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
                <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
            </providers>
        </roleManager>
    </system.web>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>
run the project.
If any problem regarding this you can contact on this blog or my email id sushil.kumar86@outloook.com

No comments:

Post a Comment

Note: only a member of this blog may post a comment.