无法使用基本身份验证进行身份验证

Ben*_*Ben 2 .net authentication iis httpmodule asp.net-web-api

我有一个ASP.NET WebApi服务,需要http基本身份验证(这是为了演示,而不是生产,所以这是基本身份验证的原因,而不是更安全的东西).Visual Studio IIS Express服务器运行良好,并通过自定义HTTP模块进行身份验证.

当我将站点部署到托管服务器时,它会失败并继续弹出登录屏幕.我向Fiddler验证了正在发送请求并且正在发送凭据.但它继续响应401未经授权的响应.似乎请求凭据在从客户端到服务器的时间内以某种方式丢失.我花了很多时间来尝试诊断这一点,使用Web API和IIS进行.NET身份验证似乎非常混乱.请帮忙!!

来自Fiddler的传出请求显示:

获取mywebsiteaddress HTTP/1.1
主机:我的网站地址
User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64; rv:21.0)Gecko/20100101 Firefox/21.0
接受:/
Accept-Language:en-US,en; q = 0.5
Accept-Encoding:gzip,deflate
授权:基本YmJvbm5ldDE4Om9jdG9iZXIxNw ==
X-Requested-With:XMLHttpRequest
Referer:mysite
连接:keep-alive

以下是我的配置的相关部分(如果需要,我可以发布更多):

<modules>
  <add name="BasicAuthHttpModule" type="ITMService.Modules.BasicAuthHttpModule"/>
</modules>

<httpModules>
  <add name="BasicAuthHttpModule" type="ITMService.Modules.BasicAuthHttpModule"/>
</httpModules>

<authentication mode="Windows"/>
Run Code Online (Sandbox Code Playgroud)

我的自定义http模块(在visual studio的测试中工作正常).这主要取自asp.net上示例:

namespace ITMService.Modules
{
public class BasicAuthHttpModule : IHttpModule
{

    private const string Realm = "www.mysite.net";

    public void Init(HttpApplication context)
    {
        context.AuthenticateRequest += OnApplicationAuthenticateRequest;
        context.EndRequest += OnApplicationEndRequest;

    }

    private static void SetPrincipal(IPrincipal principal)
    {
        Thread.CurrentPrincipal = principal;
        if (HttpContext.Current != null)
        {
            HttpContext.Current.User = principal;
            Log.LogIt("current principal: " + principal.Identity.Name);
        }
    }


    private static bool CheckPassword(string username, string password)
    {
        string passHash = AuthUser.GetUserPassword(username);
        if (PasswordHash.ValidatePassword(password, passHash))
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    private static bool AuthenticateUser(string credentials)
    {
        bool validated = false;

        try
        {
            var encoding = Encoding.GetEncoding("iso-8859-1");
            credentials = encoding.GetString(Convert.FromBase64String(credentials));

            int separator = credentials.IndexOf(':');
            string name = credentials.Substring(0, separator);
            string password = credentials.Substring(separator + 1);

            validated = CheckPassword(name, password);

            if (validated)
            {
                var identity = new GenericIdentity(name);
                SetPrincipal(new GenericPrincipal(identity, null));
            }
        }
        catch (FormatException)
        {
            // Credentials were not formatted correctly.
            validated = false;
            Log.LogIt("not validated");

        }
        return validated;
    }

    private static void OnApplicationAuthenticateRequest(object sender, EventArgs e)
    {

        var request = HttpContext.Current.Request;
        var authHeader = request.Headers["Authorization"];
        if (authHeader != null)
        {

            var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);

            // RFC 2617 sec 1.2, "scheme" name is case-insensitive
            if (authHeaderVal.Scheme.Equals("basic",
                    StringComparison.OrdinalIgnoreCase) &&
                authHeaderVal.Parameter != null)
            {

                AuthenticateUser(authHeaderVal.Parameter);
            }
        }
    }

    // If the request was unauthorized, add the WWW-Authenticate header 
    // to the response.
    private static void OnApplicationEndRequest(object sender, EventArgs e)
    {

        var response = HttpContext.Current.Response;
        if (response.StatusCode == 401)
        {
            response.Headers.Add("WWW-Authenticate",
                string.Format("Basic realm=\"{0}\"", Realm));
        }
    }

    public void Dispose()
    {
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我的IIS服务器以集成管道模式托管并运行.NET 4.我禁用了表单身份验证和禁用模拟.我在服务器上启用了基本身份验证和匿名身份验证方法.

我已经阅读了无数的论坛回复和关于此的帖子,没有什么能让我得到一个明确的答案.

Bad*_*dri 6

我看到两个www-Authenticate响应头.我相信您的HTTP模块正在添加一个,而IIS正在添加一个.确保在IIS中禁用所有类型的身份验证,如此.我的猜测是你在IIS中启用了基本身份验证.

在此输入图像描述