简体   繁体   English

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

[英]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.

相关问题 将从Android应用程序发送的用户名和密码读入我的WCF REST服务中? - Reading username and password sent from and Android app into my WCF REST Service? 如何将标头中的用户名/密码传递给 SOAP WCF 服务 - How can I pass a username/password in the header to a SOAP WCF Service 具有SSL和用户名和密码身份验证的自主WCF服务 - Selfhosted WCF Service with SSL and Username and Password authentication WCF:将用户名和密码传递给另一个服务(无Https) - WCF: Passing Username and password to another service (No Https) 我的逻辑服务层或控制器应该在哪里 - Where should be my logic Service Layer OR Controller 我应该将所有WCF服务代码包装在try catch块中吗? - should I wrap all my WCF service code in a try catch block? 我一直在客户端获得WCF服务处于故障状态。 如何在不破坏WCF服务的情况下捕获WCF异常? - I keep getting WCF service is in faulted state on the client side. How should I catch WCF exceptions, without breaking my WCF service? 验证后读取 WCF 服务中的用户名 - Read username in WCF service after authenticate 我的第一个WCF服务和我被困住了 - My First WCF Service and I'm Stuck 我可以更改WCF服务的userPrincipalName吗? - Can I change the userPrincipalName of my WCF service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM