Login控件

来源:互联网 发布:神的记事本小说9淘宝 编辑:程序博客网 时间:2024/04/30 10:00

第一步:- 在拖拽 Login 控件到页面上这样在设计视图中它会看起来像一个登陆页面。

 

图1.3:设计视图下的 Login 控件

第二步:一旦 UI 设置好了就该转到代码部分来看如何给控件写代码了。

为这个控件编写响应代码,我们首先要处理 Login1_Authenticate 事件。

双击 Login 控件将会生成如下代码:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
          bool Authenticated = false;
          Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
          e.Authenticated = Authenticated;
          if (Authenticated == true)
          {
                    Response.Redirect("Home.aspx");
          }
}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
          bool boolReturnValue = false;
          // Insert code that implements a site-specific custom
          // authentication method here.
          // This example implementation always returns false.
          string strConnection = "server=dtpxp-skumari;database=master;uid=sa;pwd=;";
          SqlConnection Connection = new SqlConnection(strConnection);
          String strSQL = "Select * From Employee";
          SqlCommand command =new SqlCommand(strSQL, Connection);
          SqlDataReader Dr;
          Connection.Open();
          Dr=command.ExecuteReader();
          while (Dr.Read())
          {
                    if ((UserName == Dr["name"].ToString()) & (Password == Dr["Password"].ToString()))
                    {
                             boolReturnValue = true;
                    }
                    Dr.Close();
                    return boolReturnValue;
          }
}

 

----------------------------------------------------------------------------------------------------------------------------------------
 Asp.net2.0 Login控件连接sql2000或sql2005数据库       
导读:
  
  在asp.net2.0中,提供了各类的provider,有membership,role,profile等的,可以很方便地实现如角色,用户等管理,甚至不用自己写代码了。而在vs.net2005中,默认的各类的provider是使用sql express的,有的时候,如果想使用sql sever 2000或者sql server2005,那应该怎么办呢?下面举例子说明:
  首先,要正确使用 provider,我们要到C:/WINNT/Microsoft.NET/Framework/v2.0.50215下,运行一个
  叫aspnet_regsql的工具,之后按照其步骤设置就可以了,最后会产生一个叫aspnetdb的数据库。
  在web.config中的配置如下:
  
  
  providerName="System.Data.SqlClient" />  providerName="System.Data.SqlClient" />
  
  
  另外:单独sql2000或sql2005 access数据库的连接:
  sqlserver:
  
   providerName="System.Data.SqlClient" />  providerName="System.Data.SqlClient" />
  
  access:
  
  
  
  连接字符:
  private static string CONNECTIONSTR = ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString; //定义数据库连接字段

 

----------------------------------------------------------------------------------------------------------------------------------

LOGIN给我们带来了方便,以下为个人的一点整理

 <connectionStrings>
<!--用户登录控件相关   aspnet_regsql 命令即可  -->
    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" connectionString="Data Source=.;Initial Catalog=SimpleOA;Persist Security Info=True;User ID=sa;Password=luxin145" providerName="System.Data.SqlClient" />
 
  <add name="SimpleOAConnectionString" connectionString="Data Source=.;Initial Catalog=SimpleOA;Integrated Security=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>

 

 

 
 
 <system.web>
    <!--更改login控件对密码安全性的要求-->
    <membership>
      <providers>
        <remove name="AspNetSqlMembershipProvider"/>
        <add name="AspNetSqlMembershipProvider"
        type=" System.Web.Security.SqlMembershipProvider,  System.Web,  Version=2.0.0.0,  Culture=neutral,  PublicKeyToken=b03f5f7f11d50a3a"
        connectionStringName="LocalSqlServer"
        enablePasswordRetrieval="false"
        enablePasswordReset="true"
        requiresQuestionAndAnswer="true"
        applicationName="/"
        requiresUniqueEmail="false"
        passwordFormat="Hashed"
        maxInvalidPasswordAttempts="5"
        minRequiredPasswordLength="6"
        minRequiredNonalphanumericCharacters="0"
        passwordAttemptWindow="10"
        passwordStrengthRegularExpression="" />
      </providers>
    </membership>