Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AuthHelper
- {
- protected readonly Logger Logger = LogManager.GetCurrentClassLogger();
- public HttpContext CurrentContext { get; set; }
- public bool IsAuthenticated { get { return CurrentContext.User != null && CurrentContext.User.Identity.IsAuthenticated; } }
- public AuthHelper(HttpContext currentContext)
- {
- if (currentContext == null)
- {
- throw new ArgumentNullException("currentContext");
- }
- CurrentContext = currentContext;
- }
- public AuthHelper():this(HttpContext.Current) { }
- public bool IsInRole(string roleName)
- {
- return IsAuthenticated && CurrentContext.User.IsInRole(roleName);
- }
- public void Logout(bool redirectToRoot)
- {
- Logger.Debug("AuthHelper.Logout: Logout current user, redirect to root:{0}", redirectToRoot);
- FormsAuthentication.SignOut();
- //sometimes session could be null EX: Server.Execute
- if (CurrentContext.Session != null)
- {
- CurrentContext.Session.Abandon();
- }
- HttpCookie c = CurrentContext.Request.Cookies[FormsAuthentication.FormsCookieName];
- if (c != null)
- {
- c.Expires = DateTime.Now.AddDays(-1);
- CurrentContext.Response.Cookies.Add(c);
- }
- if (redirectToRoot)
- {
- CurrentContext.Response.Clear();
- CurrentContext.Response.Redirect("~/",false);
- CurrentContext.ApplicationInstance.CompleteRequest();
- }
- }
- public string GetLoggedUserName()
- {
- string rtn = string.Empty;
- if (CurrentContext.User != null)
- {
- if (CurrentContext.User.Identity.IsAuthenticated)
- {
- var gp = CurrentContext.User as GenericPrincipal;
- if (gp!=null)
- {
- rtn = gp.Identity.Name;
- }
- }
- }
- Logger.Debug("AuthHelper.GetLoggedUserName: {0}",rtn);
- return rtn;
- }
- public void AuthenticateUser(string userName, string roles)
- {
- Logger.Debug("AuthHelper.AuthenticateUser: userName:{0} roles:{1}", userName, roles);
- if (string.IsNullOrEmpty(userName))
- {
- throw new ArgumentNullException("userName", "AuthHelper.AuthenticateUser: userName cannot be null");
- }
- Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
- var authenticationSection = (AuthenticationSection)config.GetSection("system.web/authentication");
- if (authenticationSection.Mode != AuthenticationMode.Forms)
- {
- throw new NotImplementedException(string.Format("AuthHelper cannot handle {0}", authenticationSection.Mode));
- }
- var timeout = authenticationSection.Forms.Timeout;
- var ticket = new FormsAuthenticationTicket(
- 1,
- userName,
- DateTime.Now,
- DateTime.Now.Add(timeout),
- true,
- roles,
- FormsAuthentication.FormsCookiePath);
- string hash = FormsAuthentication.Encrypt(ticket);
- var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
- cookie.Domain = FormsAuthentication.CookieDomain;
- if (ticket.IsPersistent)
- {
- cookie.Expires = ticket.Expiration;
- }
- CurrentContext.Response.Cookies.Add(cookie);
- SetIdentity(CurrentContext, new FormsIdentity(ticket), roles);
- }
- public static void AuthenticateRequest(HttpContext httpContext)
- {
- if (httpContext.User != null)
- {
- if (httpContext.User.Identity.IsAuthenticated)
- {
- var formsIdentity = httpContext.User.Identity as FormsIdentity;
- if (formsIdentity != null)
- {
- SetIdentity(httpContext, formsIdentity, formsIdentity.Ticket.UserData);
- }
- }
- }
- }
- private static void SetIdentity(HttpContext httpContext, IIdentity identity, string userRoles)
- {
- string[] roles = userRoles.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- httpContext.User = new GenericPrincipal(identity, roles);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment