[英]Where should I read my username and password for my WCF Service?
I have a WPF application that has just one button. 我有一个只有一个按钮的WPF应用程序。 When the button is clicked, all it does is open the service. 单击该按钮时,它所做的就是打开该服务。 Here is the code: 这是代码:
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)
{
}
}
As you can see from above, I am reading the username and password from a Credentials node in an xml file to validate the client. 从上面可以看到,我正在从xml文件中的Credentials节点读取用户名和密码以验证客户端。 Is it proper to have it located here, because originally, I had it defined in my Validate method: 将它放在这里是否合适,因为最初,我在我的Validate方法中定义了它:
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");
}
But the problem with the above is that whatever I pass into c.ClientCredentials.UserName.UserName and c.ClientCredentials.UserName.Password gets overriden when it reaches the Validate method. 但是上面的问题是,无论我传递到c.ClientCredentials.UserName.UserName和c.ClientCredentials.UserName.Password到达Validate方法时都会被覆盖。 For example, in my button click, if I just have: 例如,在我的按钮单击中,如果我只有:
c.ClientCredentials.UserName.UserName = "test1";
c.ClientCredentials.UserName.Password = "test1";
The above should fail, but when it goes into the Validate method where I read the xml file that has the username and password attribute as test and test, it will pass. 上面应该会失败,但是当它进入Validate方法,我读取了具有用户名和密码属性作为test和test的xml文件时,它将通过。
As a side note, I noticed that my Validate method gets called, but I can't seem to step into. 作为旁注,我注意到我的Validate方法被调用,但我似乎无法介入。 The debugger symbols don't get loaded. 调试器符号不会被加载。
you are overwriting the parameter with your read 你正在用你的阅读覆盖参数
public override void Validate(string suppliedUserName, string suppliedPassword){
// ...
string validUserName = doc.Root.Element("Credentials").Attribute("username").Value;
string validPassword = doc.Root.Element("Credentials").Attribute("password").Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.