c# - WCF 自定义授权

标签 c# wcf authorization

基本上,我正在创建我的第一个 WCF Web 服务,并且我希望实现自定义身份验证和授权。身份验证似乎运行良好,但我也希望能够使用自定义授权来存储角色和权限。

我的身份验证是通过重写 UserNamePasswordValidator 并使用 Validate 方法完成的。

Validate(string UserName, string password)

现在我尝试使用IAuthorizationPolicy 接口(interface)实现授权

public class AuthorizationPolicy : IAuthorizationPolicy
{
    private string _id;

    public string Id
    {
        get { return this._id; }
    }

    public ClaimSet Issuer
    {
        get { return ClaimSet.System; }
    }

    public AuthorizationPolicy()
    {
        _id = Guid.NewGuid().ToString();
    }

    public bool Evaluate(EvaluationContext context, ref object state)
    {
        IIdentity client = GetClientIdentity(context);
        context.Properties["Principal"] = new CustomPrincipal(client);

        return true;
    }

    private IIdentity GetClientIdentity(EvaluationContext evaluationContext)
    {
        object obj;
        if (!evaluationContext.Properties.TryGetValue("Identities", out obj))
            throw new Exception("No Identity found");

        IList<IIdentity> identities = obj as IList<IIdentity>;
        if (identities == null || identities.Count <= 0)
            throw new Exception("No Identity found");

        return identities[0];
    }
}

我还使用 IPrincipal 接口(interface)实现了 CustomPrincipal

public class CustomPrincipal : IPrincipal
{
    IIdentity _identity;
    string[] _roles;

    public CustomPrincipal(IIdentity identity)
    {
        _identity = identity;
    }

    public static CustomPrincipal Current
    {
        get
        {
            return Thread.CurrentPrincipal as CustomPrincipal;
        }
    }

    public IIdentity Identity
    {
        get { return _identity; }
    }

    public string[] Roles
    {
        get
        {
            if (_roles == null)
            {
                EnsureRoles();
            }

            return _roles;
        }
    }

    public bool IsInRole(string role)
    {
        EnsureRoles();

        return _roles.Contains(role);
    }

    protected virtual void EnsureRoles()
    {
        UserManager userManager = new UserManager();
        int userPermissions = userManager.UserPermissions(_identity.Name);

        if (userPermissions == 1)
            _roles = new string[1] { "ADMIN" };
        else
            _roles = new string[1] { "USER" };
    }
}

我的 App.Config 已按要求更新,并且按预期调用了 AuthorizationPolicy 中的 Evaluate 方法。

但是,这就是我卡住的地方。我如何从这里着手实现角色和权限?

最佳答案

我建议您选择 Message Inspector .

逻辑如下:

  1. 客户端将有一个消息检查器,它将为每个请求设置所需的 header 。
  2. 服务器端消息检查器将拦截请求,然后读取 header 并进行身份验证和授权。
  3. 您可以拥有一些服务,例如用户和角色服务,这些服务可以在服务器中调用,以验证 header 中的凭据并为该请求设置身份。
  4. 这些服务将通过 DAL 访问商店并将处于 inproc 模式。

关于c# - WCF 自定义授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245392/

相关文章:

c# - 是否只有在Lucene.net中搜索词组才能找到完全匹配?

c# - 处理用户控件,真的意味着编辑 .designer.cs 文件吗?

python - 从 suds.client 导入客户端失败

c# - 如何将枚举集合发送到 WCF 服务

asp.net-mvc-4 - ClaimsAuthorizationManager 从数据库中提取策略

authorization - 检查 Swift 3 中 MPMediaLibrary 的授权

c# - 转义字符

c# - c# 中的 stdout 与 console.write

wcf - 如何启动 WCF 服务的进程外实例?

authentication - 已启用CAS的Grails应用程序