我应该在哪里查看 WCF 服务的用户名和密码?

发布于 2024-11-07 17:10:56 字数 1853 浏览 2 评论 0原文

我有一个只有一个按钮的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-11-14 17:10:56

您正在用您读取的内容覆盖该参数

 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文