Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is my last dev, which is a module to auth user by base windows auth using 401 request
- */
- public class Module : IHttpModule
- {
- private static readonly List<string> AuthList = new List<string>();
- #region IHttpModule Implements
- public void Init(HttpApplication context)
- {
- context.BeginRequest += AddRequestHeaders;
- context.BeginRequest += CheckAuthentication;
- }
- public void Dispose()
- {
- //empty
- }
- #endregion
- #region Misc Functions
- private void AddRequestHeaders(object sender, EventArgs e)
- {
- var app = (HttpApplication) sender;
- app.Context.Response.AddHeader("X-MyModuleAuth", "true");
- }
- private void CheckAuthentication(object sender, EventArgs e)
- {
- var app = (HttpApplication) sender;
- var authHeader = app.Context.Request.Headers["Authorization"];
- if (AuthList.Contains(authHeader)) return;
- if (authHeader != null && authHeader.StartsWith("Basic"))
- {
- var encodeId = authHeader.Substring("Basic ".Length).Trim();
- var encoding = Encoding.GetEncoding("iso-8859-1");
- var strId = encoding.GetString(Convert.FromBase64String((encodeId)));
- var sep = strId.IndexOf(':');
- var user = strId.Substring(0, sep);
- var pass = strId.Substring(sep + 1);
- try
- {
- var usr = FTELNASAuthentication.ValidateUser(user, pass);
- if (usr != null)
- {
- AuthList.Add(authHeader);
- return;
- }
- }
- catch (Exception ex)
- {
- //empty for the moment.
- }
- }
- app.Context.Response.Headers["WWW-Authenticate"] = "Basic";
- app.Context.Response.StatusCode = 401;
- app.CompleteRequest();
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment