I'm currently trying to figure out how to perform manual windows authentication in our ASP.NET Application. The problem is that we have an OData service running, and use FormsAuthentication to provide the generic login mechanism and allow the PUT & DELETE verbs for OData, including form redirects.
However, for some customers we have integrated Windows Authentication to allow smooth integration for their users with the active directory. The problem now is that we want to be able to switch authentication methods without breaking the Odata service, because we depend on it.
What we're trying to do is mimic the Windows Authentication mechanics using an IhttpModule. So far we're able to toggle the feature on & off, and we get the challenge when a request is made. What I don't know is how to use the received information from the browser to perform authentication against the active directory:
This is the code we use to extract the NTLM information from the current request:
/// <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;
}
This allows us to extract the header from the request. What I need to know now is how I perform the authentication with this on the active directory.
This is the logic we use to extract the info:
// 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;
}
Hope someone can provide the anwser that I need.