2012-08-04 167 views
0

我正在使用表单身份验证。在这里我使用的是登录控件,并在(login.cs)控件中调用以下方法来验证用户。在这里,我使用本地名称作为用户名和密码,现在我想连接到数据库,以便检索用户名和密码。asp.net认证和授权

private bool Authenticateme(string username, string password, bool remberusername) 
{ 
    String localusername = "username"; 
    String localpassword = "amar"; 
    if (username.Equals(localusername) && password.Equals(localpassword)) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

回答

0

您需要实现OnAuthenticate加入这样的:

<asp:Login OnAuthenticate="AuthenticateEventHandler" />

添加事件处理程序.cs文件这样

private void OnAuthenticate(object sender, AuthenticateEventArgs e) 
{ 
    bool Authenticated = false; 
    Authenticated = SiteSpecificAuthenticationMethod(Login1.UserName, Login1.Password); 

    e.Authenticated = Authenticated; 
}

实施SiteSpecificAuthenticationMethod将根据数据库验证用户。

以下是参考链接:

  1. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.authenticate.aspx
  2. http://forums.asp.net/t/1403132.aspx

让我知道如果你需要更多的帮助。

-1

请尝试以下,并与现场的用户名和密码

SQlConnection con = new SqlConnection("Database connection string"); 
const string uName="UserName"; 
const string pWord="Password"; 
public bool getAuthenticate(string UserName,string Password) 
{ 
    con.Open(); 

    SqlCommand cmd = new SqlCommand(con); 
    SQlParameter[] param=new SqlParameter[]{ 
     new SqlParamter(uName,UserName), 
     new SqlParameter(pWord,Password) 
    }; 
    cmd.Parameters.AddRanage(param); 
    SqlDataReader rd; 
    rd = cmd.executeReader(); 

    if(rd.read()) 
    { 
     con.Close(); 
     return true; 
    } 
    else 
    { 
     con.Close(); 
     return false; 
    } 
} 
+1

你好SQL注入创建一个SQL Server数据库! – Vedran 2012-08-04 08:37:47

+0

SQL注入,不处理对象,全局连接对象... – 2012-08-08 11:55:20