2011-11-18 106 views
1

我有一個web應用程序(在VS2008 C#ASP.NET 3.5框架)。在我的登錄頁面有一個函數CheckLogin()執行登錄功能。我使用了遠程web服務。 objWeb是該web服務的對象。 WebService_CheckLogin是我的遠程web服務中的web方法。數據庫連接字符串寫在我的websrvice的類文件中。如何確保Webservice的安全性?

public DataSet CheckLogin() 
    { 
     string username = Convert.ToString(txtUname.Text); 
     string password = Convert.ToString(txtPassword.Text); 
     return objWEB.WebService_CheckLogin(username,password); 

    } 

WEBMETHOD在我的web

[WebMethod] 
    public DataSet WebService_CheckLogin(string uname,string pswd) 
     { 
      c.connect(); 
      DataSet ds = new DataSet(); 
      SqlCommand cmd = new SqlCommand("sp_verifyuser", c.con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@Username", uname); 
      cmd.Parameters.AddWithValue("@Password", pswd); 
      c.getdataset(cmd, ref ds); 
      return ds; 
     } 

在web服務

public void connect() 
    { 
     if (con.State == ConnectionState.Open) 
     { 
      con.Close(); 

     } 
     con.ConnectionString="Data Source=xxxxxxx;Initial Catalog=xx;User ID=xx;Password=xxxx"; 

     con.Open(); 
    } 

我的問題我的連接類是 '有什麼不對按照安全'?我的意思是'任何人都可以通過它的url來接受我的web服務'?我有我傳遞的字符串作爲參數就像

string profilePassword = objWEB.Verify_ProilePassword("exec sp_verify_profilepwd '" + txt_profil_pwd.Text + "','"+cid+"'"); 

問候, 大衛

回答

2

這一切都取決於你如何保護您的WebMethods許多其他webMethods的。您上面的代碼可能會檢查用戶是否擁有有效的用戶名/密碼組合,但很難從那裏知道您正在使用它做什麼。

你已經authenticated a user和EnableSession後一個WebMethod,你可以做這樣的事情:

[WebMethod(EnableSession = true)] 
public static string HelloWorld(){ 
if (User.Identity.IsAuthenticated) 
{ 
    return "Hello " + HttpContext.Current.Request.User.Identity.Name; 
} 
else 
{ 
    return "I don't talk to strangers"; 
} 
} 

隨意張貼任何問題。

此外 - 如果你不小心你的sp_verifyuser存儲過程是這樣的調用可能會導致災難:

WebService_CheckLogin("*","*") 
+0

@格雷格......所以如何應該是我CheckLogin()? –