简体   繁体   English

Web服务请求身份验证

[英]Web service request authentication

We're being really stuck here so I decided to ask your help. 我们真的被困在这里,所以我决定请你帮忙。

Yesterday I've been asked to help to consume a web service, got the URL to the WSDL, and the user credentials to use. 昨天我被要求帮助使用Web服务,获取WSDL的URL以及要使用的用户凭据。 I've never really had anything to do with web services, but having a general idea about them and seeing a few examples I thought it can't be that bad. 我从来没有真正与Web服务有任何关系,但对它们有一个大概的想法并看到一些例子,我认为它不会那么糟糕。 Obviously I was wrong as I'm stuck now. 显然我错了,因为我现在被困住了。

Everything seems to be fine, the proxy class (or client) has been generated, building up requests and sending them are fine too, apart from the authentication part. 一切似乎都很好,代理类(或客户端)已生成,构建请求并发送它们也很好,除了身份验证部分。 Which we can't seem to figure out how to do. 我们似乎无法弄明白该怎么做。

Using the: 使用:

client.ChannelFactory.Credentials.UserName.UserName = "myusername";
client.ChannelFactory.Credentials.UserName.Password = "mypassword";

doesn't seem to work. 似乎不起作用。 (When I check the BindingElementCollection returbed by the client.Endpoint.Binding.CreateBindingElements() there's no SecurityBindingElement) (当我检查由client.Endpoint.Binding.CreateBindingElements()返回的BindingElementCollection时,没有SecurityBindingElement)

I've tried so many other ways of doing it, but I think I'm missing something basic and the lack of documentaion is not really helping either. 我已经尝试了很多其他方法,但我认为我缺少一些基本的东西,缺乏文档也没有真正帮助。

So the question is: How do I send the username and password when making a call to a web service, using WCF? 所以问题是:如何使用WCF调用Web服务时发送用户名和密码?

Edit: Just to clarify, the request should contain something similar to this: 编辑:只是为了澄清,请求应包含类似于此的内容:

 <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
     <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-25763165">
        <wsse:Username>username</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">1DiaGTkOLk/CZhDaEpbkAaKRfGw=</wsse:Password>
        <wsse:Nonce>6ApOnLn5Aq9KSH46pzzcZA==</wsse:Nonce>
        <wsu:Created>2009-05-13T18:59:23.309Z</wsu:Created>
     </wsse:UsernameToken>
  </wsse:Security>

I had the same problem. 我有同样的问题。 Instead of the custom token serializer I used a MessageInspector to add the correct UsernameToken in the BeforeSendRequest method. 我使用MessageInspectorBeforeSendRequest方法中添加正确的UsernameToken ,而不是自定义标记序列化BeforeSendRequest I then used a custom behavior to apply the fix. 然后我使用自定义行为来应用修复。

The entire process is documented (with a demo project ) in my blog post Supporting the WS-I Basic Profile Password Digest in a WCF client proxy . 在我的博客文章中记录整个过程(带有演示项目 )在WCF客户端代理中支持WS-I Basic Profile Password Digest Alternatively, you can just read the PDF . 或者,您只需阅读PDF即可

If you want to follow my progress through to the solution, you'll find it on StackOverflow titled, " Error in WCF client consuming Axis 2 web service with WS-Security UsernameToken PasswordDigest authentication scheme ": 如果您想跟进我的进展到解决方案,您将在StackOverflow上找到它,标题为“ WCF客户端使用WS-Security UsernameToken PasswordDigest身份验证方案消耗Axis 2 Web服务时出错 ”:

I've achieved similar, using a regular HttpCookie. 我使用常规的HttpCookie实现了类似的功能。

To create the cookie: 要创建cookie:

[OperationContract]     
public void LoginToApi(string username, string password, string clientName)
{
// authenticate with DB, if successful ...
// construct a cookie
    HttpCookie httpCookie = new HttpCookie("SessionID","whateverneeded");
    HttpContext.Current.Response.SetCookie(httpCookie);
}

This appears in your regular HttpRequests, too. 这也出现在您的常规HttpRequests中。 So you just reverse the process, checking the hash/session ID/username/password whatever you put in the cookie on receipt before doing anything. 所以你只需要反转这个过程,检查哈希/会话ID /用户名/密码,无论你在收到cookie之前放入什么,都要做任何事情。

var factory = new ChannelFactory<IService>('*');
factory.Credentials.UserName.UserName = 'bob';
factory.Credentials.UserName.Password = 'bob';
var proxy = factory.CreateChannel();

For more information you can explore Authorization In WCF-Based Services*( http ://msdn.microsoft.com/en-us/magazine/cc948343.aspx )* 有关更多信息,您可以浏览基于WCF的服务中的授权*( http://msdn.microsoft.com/en-us/magazine/cc948343.aspx )*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM