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

甚至可以在Web应用程序上实现此操作,还是需要在Web角色上托管它?无论如何,如何根据自己的场所在WCF上实现简单的安全性?

最佳答案

详细的答案示例

经过一般性讨论之后,这是建立传输安全性+简单密码的详细示例(在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

问候

09-10 02:18
查看更多