【问题标题】:Adding simple security to SOAP WCF hosted on Azure webapp向 Azure webapp 上托管的 SOAP WCF 添加简单的安全性
【发布时间】:2015-11-02 11:21:24
【问题描述】:

我在一个 azure Web 应用程序上托管了一个 SOAP WCF。该服务将仅由服务器使用,不包含 UI。我只需要一个服务帐户来验证我的 WCF。我不能使用 oauth,因为它是 SOAP。我已经阅读了一些关于 ACS 的内容,但在我的情况下这似乎有点过头了,因为我只想使用一个帐户来保护我的 WCF。我的想法是我将利用 Azure AD 在那里创建一个服务帐户并使用它来保护服务。

这甚至可以在网络应用程序上实现,还是我需要在网络角色上托管它?无论如何,我如何根据我的场所在我的 WCF 上实现简单的安全性?

【问题讨论】:

  • 是的,这很可能是因为 WCF 服务涉及许多安全问题。它们可以托管在 Azure 网站中(对端口 80/443 的限制)。安全性到底是什么意思:识别客户端、发送消息、识别服务器……?
  • 我想使用某种 wcf 安全性,这样 WCF 就不会完全暴露给任何人使用。然后,如果它的 wsfederation/bearer/trust 或其他无关紧要。基本上我想知道我应该使用哪种 WS-security 方法来处理我的用例,以及我应该验证什么以及它是如何配置的。
  • 您可以使用 SWT 和 ACS 作为 Auth 提供者来创建一个非常简单的 ACS 设置。为什么你觉得 ACS 太过分了?
  • @Aleve,我只是一个详细的答案,希望对您有所帮助。它还包括一个完整的解决方案作为链接。告诉我是不是太简单了。但是,如果您想要一些角色和声明性权限,可能应该是另一个问题

标签: c# web-services wcf azure soap


【解决方案1】:

详细答案示例

经过一般讨论,这里有一个建立传输安全+简单密码的详细示例(在IIS,本地或Azure中我刚刚测试过)

这非常简单
- 没有角色,没有基于身份的声明或程序控制。
- 身份是硬编码的。
- 不使用更强的消息安全性(中间人)。
- 传输安全性是最低要求,因为基本身份验证不安全。

该安全方案实施起来很短

1.创建具有传输安全性的 Web 服务

 <system.serviceModel>
 <bindings>
  <basicHttpBinding>
    <binding name="BasicBindingConfiguration">
      <security mode="Transport">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </basicHttpBinding>
 </bindings>
<services>
  <service name="HelloServiceLibrary.HelloService" behaviorConfiguration="customIdentificationBehavior">
    <endpoint address=""
              binding="basicHttpBinding"
              contract ="HelloServiceLibrary.IHelloService"
              name="basicEndpoint"
              bindingConfiguration="BasicBindingConfiguration">
    </endpoint>

2。声明一个模块以查找 Basic-Auth

<system.webServer>
  <modules>
    <add name="BasicAuthenticationModule"
         type="Security.UserNameModuleAuthenticator,App_Code/Security" />
  </modules>
</system.webServer>  

3.模块的实现:

public class UserNameModuleAuthenticator : IHttpModule{
    ...
    public void OnAuthenticateRequest(object source, EventArgs eventArgs){
      HttpApplication app = (HttpApplication)source;
      string authStr = app.Request.Headers["Authorization"];
      string username = ...; // from header 
      string password = ...; // from header 
      if (username == "gooduser" && password == "password")
            {
                app.Context.User = new GenericPrincipal(new GenericIdentity(username, "Custom Provider"), null);
            }
            else
            {
                DenyAccess(app);
                return;
            }

4 配置客户端以通过基本身份验证

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="basicEndpoint">
        <security mode="Transport" >
          <transport clientCredentialType="Basic"
                     proxyCredentialType="None"
                     realm="" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="https://localhost/TransportUsernameService/HelloService.svc"
      binding="basicHttpBinding" bindingConfiguration="basicEndpoint"
      contract="SecureServiceReference.IHelloService" name="basicEndpoint" />
  </client>
</system.serviceModel>

5.在客户端将**凭据传递给服务器**

HelloServiceClient client = new HelloServiceClient("basicEndpoint",
    new EndpointAddress("https://testsecurewebservice.azurewebsites.net/HelloService.svc"));

client.ClientCredentials.UserName.UserName = userName;
client.ClientCredentials.UserName.Password = password;
String msg = client.SayHello(userName);

可能的扩展

  • 创建/管理一些用户(使用 ASP.Net Provider 或自定义库)
  • 有一些角色
  • 为方法设置一些声明性权限,例如:
[PrincipalPermission(SecurityAction.Demand, Role = "Manager")]

这里有完整的解决方案:http://1drv.ms/1Q5j9w0

问候

【讨论】:

  • 非常感谢!在 azure webapp 上用肥皂试了一下,它似乎工作正常!您能否解释一下使用这种解决方案在安全性方面的缺点?
  • 我在另一条消息中解释了它:“但强烈建议您也使用一些消息安全性而不是传输安全性。传输 (Https/SSL) 可能会受到中间人的攻击(控制路由器)。”
  • 如果你害怕中间人攻击,你应该使用具有消息安全性的wsHttpBinding。应该有用于加密的 X509 证书。
  • 谢谢,我最终用一个模块装订了我的整个 webapp。我使用了您提供的示例的变体。
【解决方案2】:

对于身份验证,您可以使用:

  • 通过 {login,password} 的用户名身份验证。
  • 用于识别客户端的 X509 机制(需要在客户端上部署证书)
  • 自定义身份验证

对于转移安全,您可以使用:

  • 使用安装在服务器端的证书的消息安全性
  • 传输安全 (HTTPS)

但强烈建议您同时使用一些消息安全性而不是传输安全性。 中间人(控制路由器)可以攻击传输(HTTPS/SSL)。

消息安全的缺点是你必须在服务器上安装证书

在网络角色中设置证书要容易得多,您可以在 Role.OnStart 方法中设置

  • 如果您热衷于 Web 应用程序,这里有一个使用用户名提供传输安全性的链接
    (您应该跳过使用 ASP.NET MemberShip/Role 提供程序的部分,因为您需要一个用户并且数据库是额外的工作):

https://msdn.microsoft.com/en-us/library/ff649647.aspx

  • 如果您热衷于消息安全,您应该转到 Web 角色并使用证书来确保消息安全。

具有消息安全性的自定义身份验证链接:
http://www.codeproject.com/Articles/33872/Custom-Authorization-in-WCF

问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多