1

この .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>

このサービスでは、次を使用して誰がログインしているかを確認できます。

OperationContext.Current.ServiceSecurityContext.PrimaryIdentity;

ユーザーが特定のロールのメンバーであることを確認したいので、次のように有効にしようとしましたroleManager:

   <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
      <providers>
        <clear/>
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

次に、を使用して役割を確認しますRoles

Roles.IsUserInRole(@"DOMANIN\Role");
or:
var id = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity;
Roles.IsUserInRole(id.Name, @"DOMANIN\Role");

しかし、どちらも例外をスローします:

「メソッドは、ユーザー名パラメーターが現在の Windows ID のユーザー名と一致する場合にのみサポートされます。」

また、ロールをチェックする属性がある場合は、自分で処理するのではなく、適切な例外をスローするので理想的です。

アップデート

私はちょうどこれを試しました:

if (Roles.RoleExists(@"DOMANIN\Role"))

別の例外がありました:

「構成されたロール プロバイダー (WindowsTokenRoleProvider) は、Windows 認証に依存して、ユーザーがメンバーになることを許可されているグループを決定します。ASP.NET ロール マネージャーを使用して Windows ユーザーとグループを管理することはできません。必要に応じて SQLRoleProvider を使用してください。カスタムのユーザー/役割の割り当てをサポートします。" これは、私のためにこれを見ている人の助けになるかもしれないと思っただけです。

4

2 に答える 2

1

例外は、現在ログインしているユーザーのロールのみを確認できることを示しています。それらのリストを文字列形式で取得するには、これでうまくいくはずです:

Roles.GetRolesForUser()

ただし、フォーム認証を使用する場合のように、任意のユーザーのロールを取得することはできません。

于 2012-11-13T14:31:08.770 に答える
0
[PrincipalPermission(SecurityAction.Demand, Role = @"DOMAIN\Role")]
public string DoWork()
{
    //user is authenticated and in the role
}

誰かが例外に光を当てることができれば、まだ興味があります。

これはroleManager有効になっていない場合でも機能するため、明らかに異なるメカニズムが機能しています。

于 2012-11-13T14:14:44.887 に答える