繁体   English   中英

基本身份验证和 WCF REST 服务配置问题

[英]Trouble with Basic Authentication and WCF REST Service Configuration

几天来,我一直试图弄清楚这一点。 我知道我很接近但无法弄清楚我错过了什么。

  • 该服务位于 IIS 上托管的网站中
  • 为站点启用基本身份验证,禁用包括匿名身份验证在内的所有其他身份验证。
  • 我创建了一个自签名证书并设置了 https 绑定
  • 我已经覆盖了 UsernamePasswordValidator 的 Validate 方法,但是当我附加一个断点时它没有到达。 所以我的问题可能与此有关。
  • 当我尝试在https://localhost/Services/JobSiteService.svc/rest/get访问我的服务方法时,我不断提示输入用户名和密码,因为我收到 401 未经授权的错误。

谁能看到我做错了什么?

合适的代码如下:

网页配置

  <system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="webHttpTransportSecurity">
      <security mode="Transport">
        <transport clientCredentialType="Basic"  />
      </security>
    </binding>
  </webHttpBinding>
</bindings>

<services>
  <service name="Validity.WebService.JobSiteService" behaviorConfiguration="SecureRestBehavior">
    <endpoint address="rest"  binding="webHttpBinding" behaviorConfiguration="RESTBehavior" bindingConfiguration="webHttpTransportSecurity" contract="Validity.WebService.Contracts.IJobSiteService"  />
    <endpoint address="meta" binding="mexHttpsBinding" contract="IMetadataExchange" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="SecureRestBehavior">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>

      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom"  customUserNamePasswordValidatorType="Validity.WebService.IdentityValidator, Validity.WebService" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>

  <endpointBehaviors>
    <behavior name="RESTBehavior" >
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

<protocolMapping>
  <add binding="webHttpBinding" scheme="https" />
</protocolMapping>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

服务合约:

    [ServiceContract]
public interface IJobSiteService
{
    [OperationContract]
    [WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json)]
    List<JobSite> Get();
}

自定义验证器(它在命名空间“Validity.WebService”中,但是当我包含它时代码格式被破坏了。):

        public override void Validate(string userName, string password)
    {
        using (var context = new ValidityContext())
        {

            using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)))
            {
                var user = userManager.Find(userName, password);
                if (user == null)
                {
                    var msg = String.Format("Unknown Username {0} or incorrect password {1}", userName, password);
                    throw new FaultException(msg);//the client actually will receive MessageSecurityException. But if I throw MessageSecurityException, the runtime will give FaultException to client without clear message.
                }
                else
                {

                    SessionOperationContext.Current.Items["CurrentUser"] = user;
                }
            }
        }
    }

对此的任何帮助将不胜感激。

终于想通了这一点。

解决方案是禁用 IIS 服务器上的基本身份验证并覆盖 ServiceAuthorizationManager 的 CheckAccessCore 方法以验证用户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM