4
我有一個只有一個按鈕的WPF應用程序。點擊該按鈕後,所有操作都會打開該服務。下面是代碼:我應該在哪裏閱讀我的WCF服務的用戶名和密碼?
private void button1_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.TestServiceClient c = new ServiceReference1.TestServiceClient();
XDocument doc = XDocument.Load(@"c:\Test\Test.xml");
c.ClientCredentials.UserName.UserName = doc.Root.Element("Credentials").Attribute("username").Value;
c.ClientCredentials.UserName.Password = doc.Root.Element("Credentials").Attribute("password").Value;
try
{
c.Open();
}
catch (Exception ex)
{
}
}
正如你可以看到從上面,我讀從憑證節點的用戶名和密碼在XML文件以驗證客戶端。難道是正確的已經在這裏的位置,因爲我本來就在我的驗證方法定義:
public override void Validate(string userName, string password)
{
// XDocument doc = XDocument.Load(@"c:\Test\Test.xml");
// userName = doc.Root.Element("Credentials").Attribute("username").Value;
// password = doc.Root.Element("Credentials").Attribute("password").Value;
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(password))
throw new ArgumentNullException("password");
// check if the user is not test
if (userName != "test" || password != "test")
throw new FaultException("Username and Password Failed");
}
但上述問題是,無論我進入c.ClientCredentials.UserName.UserName和c。 ClientCredentials.UserName.Password在達到Validate方法時被覆蓋。舉例來說,在我的按鈕點擊,如果我只是有:
c.ClientCredentials.UserName.UserName = "test1";
c.ClientCredentials.UserName.Password = "test1";
上述失敗,但是當它進入驗證方法,其中,我讀了具有用戶名和密碼的XML文件屬性作爲測試和測試,它會通過。
作爲一個方面說明,我注意到我的Validate方法被調用,但我似乎無法進入。調試器符號不會被加載。
爲什麼要那樣做你自己的安全,而不是依賴於WCF的內置安全功能的參數? – 2011-05-16 15:38:15
你能澄清嗎?我正在使用覆蓋UserNamePasswordValidator的Validate方法。這不是怎麼做到的?我所做的只是從xml文件中讀取用戶名和密碼。 – Xaisoft 2011-05-16 15:40:15
否 - 執行此操作的方法是使用Windows憑據(在LAN /企業環境中)或使用例如ASP.NET會員系統。讓WCF執行所有檢查和所有事情 - 您只需設置要查找有效用戶和密碼的位置即可。 – 2011-05-16 15:54:46