Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向soap Web服务传递用户名和密码_C#_Web Services_Soap - Fatal编程技术网

C# 向soap Web服务传递用户名和密码

C# 向soap Web服务传递用户名和密码,c#,web-services,soap,C#,Web Services,Soap,我正在用c#开发一个soap Web服务。它看起来像这样: public class MyService : System.Web.Services.WebService { public MyService() { } [WebMethod] public string Hello() { return "hello"; } } 我从另一个网站添加了对该web服务的服务引用,因此我可以使用以下代码从那里访问Hello()方法: MyServiceSoapClient clien

我正在用c#开发一个soap Web服务。它看起来像这样:

public class MyService : System.Web.Services.WebService
{

public MyService()
{
}

[WebMethod]
public string Hello()
{

    return "hello";
}
}
我从另一个网站添加了对该web服务的服务引用,因此我可以使用以下代码从那里访问
Hello()
方法:

MyServiceSoapClient client = new MyServiceSoapClient();
client.Hello();
现在我需要将凭据传递给该web服务。我试过:

MyServiceSoapClient client = new MyServiceSoapClient();
    client.ClientCredentials.UserName.UserName = "test";
    client.ClientCredentials.UserName.Password = "pwd";
    client.Hello();
但是我无法在webservice(在
Hello()
方法中)中获取这些凭据

如何在Web服务中获取这些值?

您可以通过属性获取用户。这将为您提供用户名,但无法检索传递的密码,这是设计的,因为身份验证在运行Web服务之前在IIS级别进行

public class MyService : System.Web.Services.WebService
{
    public MyService()
    {
    }

    [WebMethod]
    public string Hello()
    {
        return "hello, my name is " + User.Identity.Name;
    }
}

谢谢你的回答。使用User.Identity.Name时,我得到的是windows用户名。这不是我在客户端设置的用户名。请在MSDN的页面上阅读文档,您可能需要设置身份验证类型。要检查的另一个用户名是
Context.Session.User.Identity
我不知道这是否是与
User.Identity
相同的用户。