简体   繁体   English

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

[英]custom authorization and authentication in wcf webhttpbindig

I create a simple wcf service with webhttpbinding as you can see : 如您所见,我使用webhttpbinding创建了一个简单的wcf服务:

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

and the implementation : 和实现:

   public string GetData(string value)
        {

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

        }

I want to create a custom authorization and authentication about 2 weeks but i can't.i googled a lot ,but i can find authentication for wshttpbinding not webhttpbinding . 我想在2周左右的时间内创建自定义授权和身份验证,但是我不能用google搜索很多,但是我可以找到wshttpbinding而不是webhttpbinding的身份验证。

My recent question that describe main problem : 我最近的问题描述了主要问题:

My authorize custom function in wcf doesn't execute .Should i define it in webconfig? 我在wcf中的授权自定义函数未执行。我应该在webconfig中定义它吗?

My CustomAuthorizationPolicy.Evaluate() method never fires 我的CustomAuthorizationPolicy.Evaluate()方法从不触发

You can implement the ServiceAuthorizationManager to provide authorization to your WCF service with webhttpbinding. 您可以实现ServiceAuthorizationManager来通过webhttpbinding向WCF服务提供授权。

Your code could look similar to this: 您的代码可能类似于以下内容:

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;
        }
    }
}

Then register your custom ServiceAuthorizationManager in the web.config file: 然后在web.config文件中注册您的自定义ServiceAuthorizationManager:

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

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

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