简体   繁体   English

WCF单一身份验证多个端点

[英]WCF Single Authentication Multiple Endpoints

When creating a WCF service application I've implemented UserNamePassValidator for custom authentication and this works as expected. 在创建WCF服务应用程序时,我已经实现了UserNamePassValidator以进行自定义身份验证,这可以按预期工作。

But due to the large amount of functionality on the service, I've decoupled this into different service contracts such as a stock management service, location management service, task management service etc. and I've then exposed these on different endpoints within the same service. 但是由于服务上的大量功能,我已将其分解为不同的服务合同,例如库存管理服务,位置管理服务,任务管理服务等,然后我将它们暴露在同一个不同的端点上服务。

This seems to work fine, however what I would prefer is to authenticate with one endpoint and have this session state maintained across all of the endpoints. 这似乎工作正常,但我更喜欢的是使用一个端点进行身份验证并在所有端点上维护此会话状态。 At present what happens is I authenticate to one, I can then access the functionality of that service contract but if I was to connect to another endpoint it requires me to authenticate again. 目前发生的事情是我对一个人进行身份验证,然后我可以访问该服务合同的功能,但如果我要连接到另一个端点,则需要我再次进行身份验证。

My current crutch solution is to pass the ClientCredentials between forms on the client side for authenticating, and although it's using Message security so they're encrypted over the wire this is obviously not an ideal solution. 我目前的crutch解决方案是在客户端的表单之间传递ClientCredentials以进行身份​​验证,虽然它使用Message安全性,因此它们通过线路加密,但这显然不是理想的解决方案。

Is there a solution to first part? 第一部分有解决方案吗? And if not, what's the best practice for storing user entered credentials in memory (during runtime) at the client side. 如果没有,那么将用户输入的凭证存储在客户端的内存(运行时)中的最佳做法是什么。

You can implement a scheme similar to WS-Federation. 您可以实现类似于WS-Federation的方案。 It is kind of Federated Security for service level. 它是服务级别的联邦安全。

  • Firstly, your Authentication endpoint should be called STS (Security Token Service). 首先,您的身份验证端点应称为STS(安全令牌服务)。 What it does is authentication and return a security token to the client. 它的作用是验证并将安全令牌返回给客户端。

  • Secondly, STS should be trusted by all the Service Endpoints. 其次,所有服务端点都应该信任STS。 When invoking the endpoints you should pass in the security token that STS provided so that the endpoints will be able to read that token and recognize that the token was issued by a trusted STS. 在调用端点时,您应该传入STS提供的安全令牌,以便端点能够读取该令牌并识别该令牌是由受信任的STS发出的。

I have implemented one with Thinktecture at https://github.com/khoanguyen/Test-WS-Federation but sorry that I didn't give explanation you will need to research a little bit about WS-Federation and Thinktecture and WIF. 我已经通过https://github.com/khoanguyen/Test-WS-Federation在Thinktecture上实现了一个,但很抱歉我没有给出解释,你需要研究一下WS-Federation和Thinktecture以及WIF。 But you should know that it is possible to do. 但你应该知道这是可能的。


A lightweight solution that I am using for REST services for mobile project is below: 我用于移动项目的REST服务的轻量级解决方案如下:

  • I set up a Authentication endpoint. 我设置了身份验证端点。 That endpoint hold a DSA private/public key pair. 该端点拥有DSA私钥/公钥对。 When client is authenticated, this endpoint generate a token and sign it with DSA private key. 对客户端进行身份验证时,此端点会生成令牌并使用DSA私钥对其进行签名。 Then I combine the signature and token together and return it as a security token to the client. 然后我将签名和令牌组合在一起,并将其作为安全令牌返回给客户端。

  • At the service endpoints, I gave them the DSA public key (from the key pair of Authentication endpoint). 在服务端点,我给了他们DSA公钥(来自身份验证端点的密钥对)。 The DSA public key is for verifying the security tokens. DSA公钥用于验证安全令牌。

  • When client call the service endpoints, it attaches the security token as a Header of HTTP message. 当客户端调用服务端点时,它会将安全性令牌作为HTTP消息的Header附加。 Then, the service endpoints read the header to retrieve the security token -> extract the token and the signature from the security token -> use DSA public to verify it. 然后,服务端点读取标头以检索安全令牌 - >从安全令牌中提取令牌和签名 - >使用DSA public进行验证。

The strategy for generating the token depends on your need. 生成令牌的策略取决于您的需求。 In my case, my token contains client's username, expiration timestamp. 就我而言,我的令牌包含客户端的用户名,到期时间戳。 By using DSA, the hacker can extract all the token's data but they cannot alter it because they must have the DSA private key to sign the altered token. 通过使用DSA,黑客可以提取所有令牌的数据,但是他们无法改变它,因为他们必须拥有DSA私钥才能签署更改后的令牌。 Our job is just keeping the private key in secret and don't leave any sensitive info (eg password) in the token. 我们的工作就是保密私钥,不要在令牌中留下任何敏感信息(例如密码)。

This is very cheap way. 这是非常便宜的方式。 I don't need to access DB to verify user, just ensure got a valid security token, token's data is just for extra need, you can even generate a random token and sign it. 我不需要访问DB来验证用户,只需确保获得有效的安全令牌,令牌的数据仅用于额外需要,您甚至可以生成随机令牌并对其进行签名。 No session state needed. 不需要会话状态。

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

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