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
Wcf 仅当用户名参数与当前Windows标识中的用户名匹配时,才支持如何使用角色-方法_Wcf - Fatal编程技术网

Wcf 仅当用户名参数与当前Windows标识中的用户名匹配时,才支持如何使用角色-方法

Wcf 仅当用户名参数与当前Windows标识中的用户名匹配时,才支持如何使用角色-方法,wcf,Wcf,我已使用此.config成功使windows身份验证与服务调用一起工作: <?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.

我已使用此.config成功使windows身份验证与服务调用一起工作:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>

    <bindings>

      <wsHttpBinding>
        <binding name="Unsecured">
          <security mode="None"/>
        </binding>

        <binding name="Secured">
          <security mode="Message">
            <message clientCredentialType="Windows"/>
          </security>
        </binding>
      </wsHttpBinding>

    </bindings>

    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="WebApplication.SecureService">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="Secured" contract="WebApplication.ISecureService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Windows"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

</configuration>
我想检查用户是否是特定角色的成员,因此我尝试启用
角色管理器

   <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
      <providers>
        <clear/>
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
但两者都抛出异常:

“仅当用户名参数与当前Windows标识中的用户名匹配时,才支持方法。”

另外,如果有一个属性来检查角色,那将是理想的,因为它将抛出正确的异常,而不是我自己处理它

更新

我刚试过这个:

if (Roles.RoleExists(@"DOMANIN\Role"))
还有一个不同的例外:

“配置的角色提供程序(WindowsTokenRoleProvider)依赖Windows身份验证来确定允许用户加入的组。ASP.NET角色管理器不能用于管理Windows用户和组。如果您希望支持自定义用户/角色分配,请使用SQLRoleProvider。”,我只是觉得这可能会对任何帮我看这个的人有所帮助

[PrincipalPermission(SecurityAction.Demand, Role = @"DOMAIN\Role")]
public string DoWork()
{
    //user is authenticated and in the role
}
如果有人能解释这些例外情况,我仍然很感兴趣

即使在启用了
角色管理器
的情况下,这项功能也能正常工作,这显然是一种不同的工作机制

如果有人能解释这些例外情况,我仍然很感兴趣


即使在启用了
角色管理器
的情况下,也可以使用不同的机制。

例外情况告诉您,您只能检查当前登录用户的角色。要获取字符串形式的列表,这应该适用于您:

Roles.GetRolesForUser()

但是,您无法像使用表单身份验证时那样为某些任意用户获取角色。

例外情况告诉您,您只能检查当前登录用户的角色。要获取字符串形式的列表,这应该适用于您:

Roles.GetRolesForUser()

但是,您不能像使用表单身份验证时那样为某些任意用户获取角色。

我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。谢谢Erix,注意我也尝试了
Roles.IsUserInRole(@“DOMANIN\Role”)对当前登录的用户有效,并收到相同的消息。我尝试了您的建议,但得到了相同的例外:“只有用户名参数与当前Windows标识中的用户名匹配时,才支持该方法。”@Weston为什么您有身份验证模式=“表单”。默认情况下,它就在那里,我也尝试了
谢谢Erix,注意我也尝试了
角色。IsUserInRole(@“DOMANIN\Role”);
对当前登录的用户有效,并收到相同的消息。我尝试了您的建议,并得到了相同的异常:“仅当用户名参数与当前Windows标识中的用户名匹配时,才支持该方法。”@Weston您为什么有身份验证模式=“表单?”“。默认情况下它就在那里,我也尝试了
Roles.GetRolesForUser()