public void SignIn(FatecIdentity user, bool createPersistentCookie) { if (user == null) throw new ArgumentNullException("user"); var now = DateTime.UtcNow.ToLocalTime(); var roles = string.Join("|", user.Roles.ToArray()); string userData = string.Format("{0};{1};{2}", user.Fullname, user.Email, roles); var ticket = new FormsAuthenticationTicket( 1, user.Name, now, now.Add(FormsAuthentication.Timeout), createPersistentCookie, userData, FormsAuthentication.FormsCookiePath); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); cookie.HttpOnly = true; if (ticket.IsPersistent) cookie.Expires = ticket.Expiration; cookie.Secure = FormsAuthentication.RequireSSL; cookie.Path = FormsAuthentication.FormsCookiePath; if (FormsAuthentication.CookieDomain != null) cookie.Domain = FormsAuthentication.CookieDomain; _httpContext.Response.Cookies.Add(cookie); }
public FatecIdentity GetByUsername(string username) { using (DirectoryEntry directoryEntry = AdminDirectoryEntry) { using (DirectorySearcher search = CreateSearcher(directoryEntry)) { search.PropertiesToLoad.AddRange(_defaultSearchProperties); search.Filter = "(sAMAccountName=" + username + ")"; var result = search.FindOne(); string login = string.Empty; string fullName = string.Empty; string email = string.Empty; string[] roles; if (result.Properties["samAccountName"].Count > 0) login = result.Properties["samAccountName"][0].ToString(); if (result.Properties["name"].Count > 0) fullName = result.Properties["name"][0].ToString(); if (result.Properties["mail"].Count > 0) email = result.Properties["mail"][0].ToString(); roles = this.GetRolesByUsername(login).ToArray(); FatecIdentity user = new FatecIdentity(login, fullName, email, roles); return user; } } }
public FatecIdentity GetAuthenticatedUser() { if (_cachedUser != null) return _cachedUser; if(!(_httpContext.User.Identity is FormsIdentity)) return null; var formsIdentity = (FormsIdentity)_httpContext.User.Identity; string[] userdata = formsIdentity.Ticket.UserData.Split(';'); string login = formsIdentity.Ticket.Name; string fullname = userdata[0]; string email = userdata[1]; string[] roles = userdata[2].Split('|'); var user = new FatecIdentity(login, fullname, email, roles); _cachedUser = user; return _cachedUser; }
public void SignOut() { _cachedUser = null; FormsAuthentication.SignOut(); }