wcf webhttpbindig中的自定义授权和身份验证

时间:2017-08-21 06:04:12

标签: c# wcf authentication authorization webhttpbinding

我用webhttpbinding创建了一个简单的wcf服务,如你所见:

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/Data/{data}")]
    string GetData(string data);

和实施:

   public string GetData(string value)
        {

                return string.Format("You entered: {0}", value);

        }

我想创建一个自定义授权和身份验证大约2周,但我可以搜索很多,但我可以找到wshttpbinding身份验证而不是webhttpbinding。

我最近提出的描述主要问题的问题:

http://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html#supported-event-source-s3

My authorize custom function in wcf doesn't execute .Should i define it in webconfig?

1 个答案:

答案 0 :(得分:2)

您可以使用webhttpbinding实现ServiceAuthorizationManager以向您的WCF服务提供授权。

您的代码可能与此类似:

public class CustomAuthorizationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        try
        {
            ServiceSecurityContext securityContext = operationContext.ServiceSecurityContext;
            WindowsIdentity callingIdentity = securityContext.WindowsIdentity;

            WindowsPrincipal principal = new WindowsPrincipal(callingIdentity);
            return principal.IsInRole("Administrators");
        }
        catch (Exception)
        {
            return false;
        }
    }
}

然后在web.config文件中注册自定义ServiceAuthorizationManager:

<serviceBehaviors>
  <behavior name="ServiceBehaviour">
    <serviceMetadata httpsGetEnabled="true"/>
    <serviceAuthorization serviceAuthorizationManagerType="YourNamespace.CustomAuthorizationManager, YourAssemblyName"/>
  </behavior>
</serviceBehaviors>