public void SetPrincipal(SecurityPrincipal principal)
 {
     if (HttpContext.Current.Session != null)
     {
         HttpContext.Current.Session["SECURITY_PRINCIPAL"] = principal;
     }
 }
Esempio n. 2
0
        public static void LogIn(NetworkCredential credential)
        {
            SecurityPrincipal principal = authenticationProvider.Authenticate(credential);

            if (principal == null || (!(principal.SecurityIdentity.IsAuthenticated)))
            {
                throw new SecurityException("User, " + credential.UserName + ", could not be authenticated.");
            }

            if (setThreadPrincipal)
            {
                System.Threading.Thread.CurrentPrincipal = principal;
            }

            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = principal;
            }

            principal.Profile = authorizationProvider.Authorize(principal);
            contextStoreProvider.SetPrincipal(principal);
        }
        public Security.SecurityPrincipal Authenticate(System.Net.NetworkCredential credential)
        {
            #region Variables
            SecurityPrincipal principal = null;
            #endregion

            using (var db = new FleetConnectEntities())
            {
                try
                {
                    #region Get user object with credentials supplied
                    string encryptedPassword = EncryptionHelper.EncryptData(credential.Password);

                    var roles = from item in db.SECURITY_Role select item;

                    //Get the user by email address
                    SECURITY_User usr = db.SECURITY_User.FirstOrDefault(u => u.EmailAddress == credential.UserName && u.Password == encryptedPassword);
                    #endregion
                    #region Verify user

                    if (usr == null)
                    {
                        log.Debug("Invalid user credentials supplied");
                    }
                    else
                    {
                        log.Info("Log in credentials validated, creating security principal for " + usr.EmailAddress);
                        principal = new SecurityPrincipal(new SecurityIdentity(usr.UserId, usr.FirstName + " " + usr.LastName), new string[0]);
                        Helpers.SessionHelpers.LOGGED_IN_USER = usr.UserId;
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    log.Error("Error authenticating user", ex);
                }
            }
            return(principal);
        }