我有一个 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 / 0101 Firefox / 21.0
接受:/
Accept-Language:en-US,en; q = 0.5
Accept-Encoding:gzip,deflate
授权:基本YmJvbm5ldDE4Om9jdG9iZXIxNw ==
X-Requested-With:XMLHttpRequest
推荐人:狡猾
连接:保持活力
以下是我的配置的相关部分(如果需要,我可以发布更多):
我的自定义http模块(在visual studio的测试中工作正常)。这主要取自example on :
namespace ITMService.Modules
{
public class BasicAuthHttpModule : IHttpModule
{
private const string Realm = "";
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()
{
}
}
}
我的IIS服务器以集成管道模式托管并运行.NET 4。我禁用了表单身份验证和禁用模拟。我在服务器上启用了基本身份验证和匿名身份验证方法。
我已经阅读了无数的论坛回复和关于此的帖子,没有什么能让我得到一个明确的答案。