Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Roles.IsUserInRole()在使用wsHttpBinding和MVC 4的WCF中不工作_Wcf_Roles_Wshttpbinding_Simplemembership - Fatal编程技术网

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

Roles.IsUserInRole()在使用wsHttpBinding和MVC 4的WCF中不工作,wcf,roles,wshttpbinding,simplemembership,Wcf,Roles,Wshttpbinding,Simplemembership,我有以下测试设置,全部正常工作: -运行MathService.svc的WCF应用程序,设置为使用SimpleMembershipProvider -使用默认SimpleMembershipProvider的MVC 4 Internet应用程序 -成员包括: 3个角色:“调试”、“管理员”和“编辑” 2个用户:角色调试中的“调试”和管理员(ya,角色调试中的用户调试) 角色“管理员”中的“管理员” -据我所知,证书正在工作,我可以使用wshttp连接到服务 服务方法代码 //[Princip

我有以下测试设置,全部正常工作:

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

-使用默认SimpleMembershipProvider的MVC 4 Internet应用程序

-成员包括:

  • 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"));  
    }
}
为什么调用“Roles.GetRolesForUser(cltName)”和IsUserInRole失败

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

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

有一些帖子建议证书//成员资格设置错误,但我看不出我怎么能走到目前为止,仍然有一个错误的设置,最重要的是,只是一些角色方法失败,而不是全部失败。有什么建议吗

关于返回结果,如果我使用我的角色解决方法并通过调试调用,服务将返回双精度,如果我在禁用admin[PrincipalPermission]的情况下调用,则返回整数精度


尊敬,Andreas,以防有人遇到同样的问题

虽然您可以将“旧”ASP.net角色提供程序与simpleMembership一起使用,但它们并不相同

在我的例子中,我必须添加一个简单的演员阵容

var _simpleRoles = (SimpleRoleProvider)Roles.Provider; //need the cast to simple
这样就行了

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

这可能会有所帮助:我按照这个示例设置了wsHTTP,是的,他们使用了所谓的“命令式角色检查”和Roles.IsUserInRole()。但它们也使用默认的ASP.NET成员角色提供程序。我使用MVC4和新的SimpleMembership提供程序。我在链接中找不到关于此特定方法不起作用的原因。在我的情况下,我必须编写
Roles.Provider.IsUserInRole()
,而不是
Roles.IsUserInRole(…)