【问题标题】:Can WCF Authentication be disabled for a single Operation是否可以为单个操作禁用 WCF 身份验证
【发布时间】:2013-09-03 03:19:00
【问题描述】:

我最近阅读了 WCF 安全指南,并正在使用它来创建一个应用程序,用户需要在其中创建自己的帐户,然后使用消息安全性访问该应用程序。下面是我从本书中得到的 web.config 文件。我遇到的问题与用户注册有关。由于应用程序需要用户名/密码验证,因此注册新用户的方法总是失败。

我知道解决此问题的一种方法(切换到基于角色的身份验证并在具有注册权限的角色中创建单个用户,并将该信息硬编码到注册方法中)但我正在寻找替代解决方案。有没有办法在操作级别声明特定方法不需要身份验证?如果没有,有没有办法用不同的服务合同公开一个单独的端点,并且该合同不需要身份验证? web.config 文件中的很多内容都与成员资格提供程序和消息安全性有关,我有点担心,但是对于在数据库中正确注册新成员的方法,它显然必须了解成员资格提供程序......

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <connectionStrings>
    <add name="MyLocalSQLServer"
         connectionString="Initial Catalog=aspnetdb;data source=DESKTOP;Integrated Security=SSPI;"/>
  </connectionStrings>
  <system.web>
    <authentication mode="Forms" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
    <membership defaultProvider="MySqlMembershipProvider">
      <providers>
        <clear />
        <add name="mySqlMembershipProvider"
             connectionStringName="MyLocalSQLServer"
             applicationName="Mech"
             type="System.Web.Security.SqlMembershipProvider" />
      </providers>
    </membership>                
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsDualHttpBinding>
        <binding name="wsDualHttpEndpointBinding">
          <security>
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <services>
      <service name="MechService.MechService">
        <endpoint address="localhost/MechService" binding="wsDualHttpBinding" bindingConfiguration="wsDualHttpEndpointBinding"
          name="wsDualHttpEndpoint" contract="MechService.IMechService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceCredentials>
            <serviceCertificate findValue="CN=TempCert" />
            <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"
              membershipProviderName="MySqlMembershipProvider" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

这是注册新用户的功能。

    public string CreateUser(String Name, String Password, String Email, String Question, String Answer)
    {
        MembershipCreateStatus status;

        try
        {
            MembershipUser newUser = Membership.CreateUser(Name, Password, Email, Question, Answer, true, out status);

            if (newUser == null)
            {
                return GetErrorMessage(status);
            }
            else
            {
                return "MECH_CODE_SUCCESS";
            }
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

【问题讨论】:

    标签: wcf authentication operationcontract


    【解决方案1】:

    您不能从单个操作中选择退出身份验证(尽管您可以选择退出授权,但这是对您有帮助的注意事项)。好消息是身份验证设置是针对每个端点的,并且端点有一个合同。因此,将 registerUser 操作移至新的 ServiceContract 并为其创建单独的绑定,无需身份验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2019-10-21
      相关资源
      最近更新 更多