C# 向soap Web服务传递用户名和密码
我正在用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
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
相同的用户。