WCF服务上的自定义基本身份验证

 白人冰娟 发布于 2023-02-07 01:41

我一直在努力争取在WCF服务上使用自定义基本身份验证,现在已经有一段时间了。

我尝试使用自定义的userNameAuthentication和自定义的serviceAuthorizationManager。

我遇到的是,我陷入了一个“循环”中,要求提供凭据,而且似乎从未获得我的自定义身份验证。根据我的研究,这似乎是因为IIS劫持了身份验证并检查本地用户。

从Web配置中,我已通过基本身份验证打开了传输安全性:


    
      
        
      
    
  

相关服务行为:(我尝试将其中一项内容注释掉)


    
      
      
      
      
      
      
    
  

授权类(永远不会被调用):

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的问题?

如果您需要更多详细信息,请告诉我,我们很乐意分享您可能遇到的任何问题的答案。

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有