简体   繁体   English

如何使用ASMX Web服务中的WS-Security和Access UsernameToken?

[英]How to use WS-Security and Access UsernameToken from an ASMX Web Service?

Okay, so we have a legacy ASMX web service that is currently running in .NET 3.5 and we're using Visual Studio 2008. 好的,我们有一个目前在.NET 3.5中运行的传统ASMX Web服务,我们正在使用Visual Studio 2008。

The problem is, we need to add authentication and would like to take advantage of the WS-Security model without breaking any existing internal clients who don't need to authenticate currently. 问题是,我们需要添加身份验证,并希望利用WS-Security模型,而不会破坏任何不需要当前身份验证的现有内部客户端。

We've thought about adding custom headers, but that's not very WS-Security-ish. 我们已经考虑过添加自定义标头,但这不是WS-Security-ish。 Also upgrading to WCF, while a long term goal, is not viable in the short-term. 升级到WCF虽然是长期目标,但短期内不可行。

Is there a way to access the UsernameToken (provided it's passed by the client) indirectly in the soap header of a VS2008 ASMX web service? 有没有办法间接访问VS2008 ASMX Web服务的soap标头中的UsernameToken(假设它是由客户端传递的)?

You could try Web Services Enhancements (WSE) 3.0 . 您可以尝试Web服务增强(WSE)3.0 This adds support for an old version of WS-Security (the 2004 version I think - WCF supports the 2005 and 2007 versions). 这增加了对旧版 WS-Security的支持(我认为2004版本--WCF支持2005和2007版本)。 It sits on top of ASMX without disturbing it, and does still work in .NET 3.5 / WS2008. 它位于ASMX的顶部而不会打扰它,并且仍然可以在.NET 3.5 / WS2008中运行。

Now for the downsides: 现在的缺点是:

  • VS2008 does not support adding or updating WSE-enabled web references in client code. VS2008不支持在客户端代码中添加或更新启用WSE的Web引用。 It will happily create the normal ASMX proxy class, but not the extra WSE proxy class that is required for authentication. 它将很乐意创建正常的ASMX代理类,但不会创建身份验证所需的额外WSE代理类。 Any existing WSE proxy code you have does compile OK, but will be deleted if you try to update the web reference in the IDE. 您拥有的任何现有WSE代理代码都可以编译正常,但如果您尝试在IDE中更新Web引用,则会将其删除。 If you possess a copy of VS2005, you could use it to maintain or at least create the web reference on the client side. 如果您拥有VS2005的副本,则可以使用它来维护或至少在客户端创建Web引用。
  • AFAIK, the WSE implementation of WS-Security is not 100% forward-compatible with the WCF implementations. AFAIK,WS-Security的WSE实现与WCF实现不是100%向前兼容。 You will need to do some compatibility testing of your own with WCF to make sure. 您需要对自己的WCF进行一些兼容性测试以确保。

Example

Specifying credentials on the client: 在客户端上指定凭据:

void SetUsernameCredential(WebServicesClientProtocol service, string userName, string password) {
    UsernameToken token = new UsernameToken(userName, password, PasswordOption.SendHashed);
    service.SetClientCredential(token);
}

Authenticating credentials on the server: 验证服务器上的凭据:

public class MyUsernameTokenManager : UsernameTokenManager {
    protected override string AuthenticateToken(UsernameToken token) {
        // Authenticate here.
        // If succeess, return an authenticated IPrincipal and the user's password as shown.
        // If failure, throw an exception of your choosing.
        token.Principal = principal;
        return password;
    }
}

Reading credentials on the server: 读取服务器上的凭据:

IPrincipal principal = RequestSoapContext.Current.IdentityToken.Principal;

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

相关问题 如何为WS-Security生成UsernameToken? - How to generate UsernameToken for WS-Security? 使用 WS-Security UsernameToken PasswordDigest 身份验证方案使用 Axis 2 Web 服务的 WCF 客户端出错 - Error in WCF client consuming Axis 2 web service with WS-Security UsernameToken PasswordDigest authentication scheme 如何在 WCF 客户端服务中实现 WS-security(时间戳、用户名令牌、签名) - How do I implement WS-security in WCF client service (timestamp, usernametoken, signature) 使用 WS-Security 使用 SOAP web 服务 - consume SOAP web service with WS-Security 如何在C#中使用WS-Security? - How to use WS-Security in C#? 如何使WCF客户端符合特定的WS-Security - 签署UsernameToken和SecurityTokenReference - How to make WCF Client conform to specific WS-Security - sign UsernameToken and SecurityTokenReference 使用WS-Security的WCF客户端调用Java Web服务 - WCF client using WS-Security to call java web service .net调用已启用WS-Security的Web服务(用Java创建) - .net call WS-Security enabled web service (created in java) 使用需要ASP.NET 4.5应用程序的WS-Security的Web服务 - Consume a Web Service that requires WS-Security from ASP.NET 4.5 Application 如何使用c#来使用受WS-Security保护的Web服务? - How do I consume a web service protected with WS-Security in mono using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM