Roles.IsUserInRole()使用wsHttpBinding和MVC 4无法在WCF中工作

And*_*eas 5 wcf roles wshttpbinding simplemembership

我有以下测试设置,所有工作:

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

-MVC 4 Internet App使用默认的SimpleMembershipProvider

- 会员资格是:

  • 3角色:'调试','管理员'和'编辑'
  • 2用户:角色调试和管理员中的"调试"(ya,角色调试中的用户调试)
  • 角色管理员中的"管理员"

- 证书,据我所知,我可以使用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"));  
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我从'ServiceSecurityContext'获得正确的用户名,如果我启用了[PrincipalPermission] attrib,如果我按照预期使用用户Admin调用该服务,我会被拒绝.

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

有一些帖子表明证书/ /成员资格设置错误,但我看不出我到目前为止如何得到错误的设置,最重要的是,只是一些角色方法失败,而不是全部.有什么指针吗?

关于返回结果的一句话,如果我使用我的角色变通方法并通过Debug调用,该服务返回双精度,如果我调用admin [PrincipalPermission]禁用,我得到整数精度

此致,安德烈亚斯

And*_*eas 4

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

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

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

var _simpleRoles = (SimpleRoleProvider)Roles.Provider; //need the cast to simple
Run Code Online (Sandbox Code Playgroud)

然后这有效

 var b = simpleRoles.IsUserInRole(cltName, "Debug"); 
Run Code Online (Sandbox Code Playgroud)