我目前正在尝试弄清楚如何在ASP.NET应用程序中执行手动Windows身份验证。问题是我们运行了OData服务,并使用FormsAuthentication提供通用登录机制并允许PUT&删除OData的动词,包括表单重定向。
但是,对于某些客户,我们集成了Windows身份验证,以便用户可以使用活动目录顺利集成。现在的问题是我们希望能够在不破坏Odata服务的情况下切换身份验证方法,因为我们依赖它。
我们要做的是使用IhttpModule模仿Windows身份验证机制。到目前为止,我们可以在&和关闭,我们在提出请求时接受挑战。我不知道的是如何使用从浏览器接收的信息对活动目录执行身份验证:
这是我们用于从当前请求中提取NTLM信息的代码:
/// <summary>
/// <para>Determines whether the current <see cref="HttpRequest"/> is a NTML challenge.</para>
/// </summary>
/// <param name="request">The <see cref="HttpRequest"/> to evaluate.</param>
/// <param name="header">The output header to authenticate.</param>
/// <returns>True if the current <see cref="HttpRequest"/> is considered a NTML challenge.</returns>
protected bool IsNtlmChallenge(HttpRequest request, out string header)
{
const string headerName = @"Authorization";
if (request.Headers.AllKeys.Contains(headerName))
{
header = request.Headers[headerName];
return true;
}
header = string.Empty;
return false;
}
这允许我们从请求中提取标头。我现在需要知道的是我如何在活动目录中使用它进行身份验证。
这是我们用来提取信息的逻辑:
// Check if we need to handle authentication through Windows authentication or not.
if (WindowsAuthentication)
{
string encryptedHeader;
// If this is a challenge from the client, perform the Windows Authentication using the
// information stored inside the header.
if(IsNtlmChallenge(HttpContext.Current.Request, out encryptedHeader))
{
/* how to authenticate here with the encrypted header? */
}
HttpContext.Current.Response.AddHeader("WWW-Authenticate", "NTLM");
HttpContext.Current.Response.StatusCode = 401;
return;
}
希望有人可以提供我需要的anwser。
答案 0 :(得分:0)
好的,
根据我的问题收到的意见,我提出了以下解决方案,以绕过我的问题。我知道这不是一个干净的解决方案,但至少它对我们有用。
这要求我们为加密生成相同的密钥。解密两个应用程序。可以使用IIS管理器中的机器密钥模块为您的应用程序设置此项。 如果两个应用程序的密钥不相同,则cookie的编码/解码过程将失败。我们将它们设置为使用SHA1自动生成,但两个应用程序使用相同的键。
现在我们检查原始登录页面上的设置,如果需要Windows身份验证,则重定向到子应用程序的登录页面并在那里执行登录。然后我们重定向回原始登录页面并使用cookie继续。
这会在进行初始登录时产生一些重定向,但之后应用程序运行顺利。