【问题标题】:Roles.IsUserInRole() not working in WCF using wsHttpBinding and MVC 4Roles.IsUserInRole() 在使用 wsHttpBinding 和 MVC 4 的 WCF 中不起作用
【发布时间】:2012-11-22 08:04:35
【问题描述】:

我有以下测试设置,一切正常:

-运行 MathService.svc 的 WCF 应用程序,设置为使用 SimpleMembershipProvider

-MVC 4 Internet App 使用默认的 SimpleMembershipProvider

-成员是:

  • 3 个角色:“调试”、“管理员”和“编辑”
  • 2 个用户:角色调试和管理员中的“调试”(是的,角色调试中的用户调试)
  • 角色管理员中的“管理员”

-证书,据我所知,我可以使用 wshttp 连接到服务

服务方法代码。

//[PrincipalPermission(SecurityAction.Demand, Role = "Debug")]
public string Add(double A, double B)
{
    OperationContext oc = OperationContext.Current;
    ServiceSecurityContext ssc = oc.ServiceSecurityContext;
    string cltName = ssc.PrimaryIdentity.Name;   //cltName = "Debug"
    var Rs = Roles.GetAllRoles(); //returns: 'Debug', 'Administrator', 'Editor' => OK
    var dUsers = Roles.GetUsersInRole("Debug");  // 'Debug' => Expected
    var aUsers = Roles.GetUsersInRole("Administrator"); // 'Debug', 'Admin' => expected
    try
    {
        var a = Roles.GetRolesForUser(cltName); //this fails 
        var b = Roles.IsUserInRole(cltName, "Debug"); //this fails 
        var c = Roles.IsUserInRole(cltName, "Administrator"); //this fails 
    }
    catch (Exception err)
    {
        string p = err.Message; // all fail with error :
        // "Object reference not set to an instance of an object", inner exception=null
    }
    if (dUsers.Contains(cltName)) //this works, but requires extra step 
        //I should be able to us if(Roles.IsUserInRole(cltName, "Debug"))... here?!?
    {
        return string.Format("Result: {0}", (A + B).ToString("N2"));
    }
    else
    {   //this is just to get a different result if NOT in role 'Debug'
        return string.Format("Result: {0}", ((int)A + (int)B).ToString("N2"));  
    }
}

为什么调用“Roles.GetRolesForUser(cltName)”和 IsUserInRole 失败?

我从“ServiceSecurityContext”获得了正确的用户名, 如果我启用 [PrincipalPermission] 属性,如果我使用用户 Admin 调用服务,我会按预期被拒绝。

那么为什么 PrincipalPermission 能够获得正确的用户角色呢? 为什么我可以使用 Roles.GetUsersInRole("Debug") 来获取所有正确的用户 但是我不能调用 Roles.IsUserInRole(..)??

有一些帖子提示证书//会员设置错误,但我看不到到目前为止我是如何做到的,但仍然有错误的设置,最重要的是,只有一些角色方法失败,而不是全部。有什么指点吗?

关于返回结果的一句话,如果我使用我的角色解决方法并通过调试调用,服务返回双精度,如果我在禁用管理员 [PrincipalPermission] 的情况下调用,我会返回整数精度

问候,安德烈亚斯

【问题讨论】:

  • 我按照该示例设置了 wsHTTP,是的,他们使用了所谓的“命令式角色检查”和 Roles.IsUserInRole()。但他们也使用默认的 ASP.NET Membership 角色提供程序。我使用 MVC 4 和新的 SimpleMembership Provider。我无法在链接中找到有关此特定方法为何不起作用的任何内容

标签: wcf roles wshttpbinding simplemembership


【解决方案1】:

以防万一有人遇到同样的问题。

虽然您可以将“旧”ASP.net RolesProvider 与 simpleMembership 一起使用,但它们并不相同。

就我而言,我必须添加一个简单的演员表。

var _simpleRoles = (SimpleRoleProvider)Roles.Provider; //need the cast to simple

然后就可以了

 var b = simpleRoles.IsUserInRole(cltName, "Debug"); 

【讨论】:

  • 就我而言,我必须写 Roles.Provider.IsUserInRole() 而不是 Roles.IsUserInRole(...)
猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多