gpt4 book ai didi

c# - 我应该在哪里阅读我的 WCF 服务的用户名和密码?

转载 作者:行者123 更新时间:2023-11-30 12:51:48 25 4
gpt4 key购买 nike

我有一个只有一个按钮的 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";

上面应该会失败,但是当它进入我读取具有用户名和密码属性的 xml 文件作为测试和测试的 Validate 方法时,它将通过。

附带说明一下,我注意到我的 Validate 方法被调用了,但我似乎无法介入。调试器符号未加载。

最佳答案

你正在用你的读取覆盖参数

 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;

关于c# - 我应该在哪里阅读我的 WCF 服务的用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6019718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com