【问题标题】:WCF CustomRoleProvider and Principle PermissionsWCF 自定义角色提供者和主体权限
【发布时间】:2012-03-09 12:18:42
【问题描述】:

编辑:我认为该问题可能与以下问题有关,因为我也在使用 SSL PrincipalPermission.Demand() failing once WCF Service was moved to SSL

我正在开发一组安全的 Web 服务,我已经实现了 CustomRoleProvider 和 CustomMembershipProvider 来对用户进行身份验证。

这很好用,但是如果用户未通过身份验证,我想限制对大多数服务调用的访问。

我计划使用以下方法来完成此操作

[PrincipalPermission(SecurityAction.Demand, Authenticated=true)]

但是,这似乎无法检测用户何时通过身份验证并且总是引发安全异常。我不确定我做错了什么。

public class CustomMembershipProvider : MembershipProvider
{
    public string UserType;

    public override bool ValidateUser(string username, string password)
    {
           //Custom logic to work out if user exists and password is correct

           //If the user exists and password matches we will get a populated user
           //object containing their username and usertype

            if (user == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }

在我的身份验证服务调用中,我检查成员资格提供程序是否返回 true 并设置表单身份验证 cookie。

        if (Membership.ValidateUser(username, password))
        {
            FormsAuthentication.SetAuthCookie(username, false);
        }

我在我的网络配置中设置了服务授权,如下所示:

    <behavior name="SecureAuthServiceBehavior">
      <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="CustomRoleProvider"/>
      .... 
     </behaviour>

任何帮助将不胜感激,谢谢

编辑:

我对该问题进行了进一步调查,发现 Principal 设置正确。 我有以下 Service 方法,在其中我得到 Principal 并检查用户是否处于正确的角色,有效地执行标签在开始时所做的事情。

[PrincipalPermission(SecurityAction.Demand,Role="A" )]
public bool DoWork()
{
    IPrincipal p = HttpContext.Current.User;
    if (p.IsInRole("A"))
    {
        return true;
    }
    else
    {
        return false;
     }
}

此方法当前每次都会抛出 SecurityException,但是如果我在开始时注释掉主体权限,则该方法将起作用并返回 true。

【问题讨论】:

    标签: wcf forms-authentication roleprovider iprincipal principalpermission


    【解决方案1】:

    PrincipalPermission 检查 Thread.CurrentPrincipal,而不是 HttpContext.Current.User,这就是为什么将 principalpermission 属性注释掉后,您的 DoWork() 返回 true,但存在该行时返回 false(因为未设置 Thread.CurrentPrincipal任何东西)。

    在我的服务类的构造函数中,我设置了 Thread.CurrentPrincipal = HttpContext.Current.User,现在它们正确匹配。 principalpermission 属性然后按照您的预期阻止/允许。

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2013-02-02
      相关资源
      最近更新 更多