2012-08-04 165 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