我应该在哪里查看 WCF 服务的用户名和密码?
我有一个只有一个按钮的 WPF 应用程序。单击该按钮时,它所做的就是打开该服务。代码如下:
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 文件中的 Credentials 节点读取用户名和密码来验证客户端。将其放置在这里是否正确,因为最初,我在 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");
}
但上述问题是,我传递给 c.ClientCredentials.UserName.UserName 和 c.ClientCredentials.UserName.Password 的任何内容都会被覆盖当它到达 Validate 方法时。例如,在我的按钮单击中,如果我只有:
c.ClientCredentials.UserName.UserName = "test1";
c.ClientCredentials.UserName.Password = "test1";
上面应该会失败,但是当它进入 Validate 方法时,我在其中读取具有用户名和密码属性的 xml 文件作为 test 和 test,一切都会过去的。
作为旁注,我注意到我的 Validate 方法被调用,但我似乎无法进入。调试器符号不会被加载。
I have a WPF application that has just one button. 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. Is it proper to have it located here, because originally, I had it defined in my Validate method:
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. 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.
As a side note, I noticed that my Validate method gets called, but I can't seem to step into. The debugger symbols don't get loaded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在用您读取的内容覆盖该参数
you are overwriting the parameter with your read