使用自定义基本身份验证在WCF服务中

5
我一直在试图让WCF服务支持自定义基本身份验证,但一直未能成功,并在互联网上寻找答案,但没有什么好的结果。
我尝试过使用自定义的userNameAuthentication和custom serviceAuthorizationManager。
然而,我遇到的问题是,我陷入了一个“循环”中,它不断地要求凭据,但从我的研究来看,似乎是因为IIS劫持了身份验证并检查本地用户。
从Web配置文件中,我已经打开了基本身份验证的传输安全性:
<webHttpBinding>
    <binding name="SecureBinding">
      <security mode="Transport">
        <transport clientCredentialType="Basic" />
      </security>
    </binding>
  </webHttpBinding>

相关服务行为:(我已经将我尝试的一件事注释掉了)

<serviceBehaviors>
    <behavior name="AuthenticatedWCF.CustomServiceBehavior">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceAuthorization serviceAuthorizationManagerType="AuthenticatedWCF.Classes.Helpers.AuthenticationManager, AuthenticatedWCF" />
      <!--<serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="AuthenticatedWCF.Classes.Helpers.AuthenticationManager, AuthenticatedWCF" />
      </serviceCredentials>-->
    </behavior>
  </serviceBehaviors>

授权类(从未被调用):
public class AuthenticationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        //Extract the Authorization header, and parse out the credentials converting the Base64 string:
        var authHeader = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];

        if ((authHeader != null) && (authHeader != string.Empty))
        {
            return true;
            /*var svcCredentials = System.Text.ASCIIEncoding.ASCII
                    .GetString(Convert.FromBase64String(authHeader.Substring(6)))
                    .Split(':');

            throw new Exception(authHeader);

            var user = new { Name = svcCredentials[0], Password = svcCredentials[1] };
            if ((user.Name == "user1" && user.Password == "test"))
            {
                //User is authrized and originating call will proceed
                return true;
            }
            else
            {
                //not authorized
                return false;
            }*/
        }
        else
        {
            //No authorization header was provided, so challenge the client to provide before proceeding:
            WebOperationContext.Current.OutgoingResponse.Headers.Add("WWW-Authenticate: Basic realm=AuthenticatedWCF");
            //Throw an exception with the associated HTTP status code equivalent to HTTP status 401
            //throw new WebFaultException("Please provide a username and password", HttpStatusCode.Unauthorized);
            throw new WebFaultException(HttpStatusCode.Unauthorized);
            //return false;
        }
    }
}

我想问题是,我该如何解决IIS的问题?

如果您需要更多细节,请让我知道,我很乐意回答任何您可能有的问题。


2
对我而言,只有在禁用IIS中的基本身份验证后,ServiceAuthorizationManager才变为活动状态。此外,应该排除或设置传输客户端凭据类型为“None”。 - ajayel
1个回答

1

您需要在web config中关闭基本身份验证才能调用此类。基本身份验证是由IIS在应用程序甚至意识到它之前处理的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接