コード例 #1
0
        public void Authen_Test_Attemp()
        {
            //Arrage
            User   u         = null;
            string idcard    = "0000000000001";
            string firstname = "budgetor";
            User   u2        = context.PersistenceSession.Get <User>(2L);

            IList <iSabaya.UserSession> userSessions = context.PersistenceSession
                                                       .QueryOver <iSabaya.UserSession>()
                                                       .Where(us => us.User.ID == u2.ID && us.SessionPeriod.To == iSabaya.TimeInterval.MaxDate)
                                                       .List();

            foreach (iSabaya.UserSession us in userSessions)
            {
                using (ITransaction tx = context.PersistenceSession.BeginTransaction())
                {
                    try
                    {
                        us.SessionPeriod.To = DateTime.Now;
                        us.Save(context);

                        tx.Commit();
                    }
                    catch (Exception ex)
                    {
                        tx.Rollback();
                    }
                }
            }

            AuthenticateManager.Authenticate(context, iSabaya.SystemEnum.RiskAssessmentAnalysisSystem, idcard, firstname, ref u);

            AuthenticateManager.AuthenState state = AuthenticateManager.Authenticate(context, iSabaya.SystemEnum.RiskAssessmentAnalysisSystem, idcard, firstname, ref u);


            //Act
            User uAct = context.PersistenceSession.Get <User>(2L);

            AuthenticateManager.AuthenState stateAct = AuthenticateManager.AuthenState.AlreadyLogin;

            //Assert
            Assert.AreEqual(state, stateAct);
            Assert.AreEqual(u, uAct);
        }
コード例 #2
0
        public void Authen_Test_Success()
        {
            //Arrage
            User   u         = null;
            string idcard    = "0000000000000";
            string firstname = "admin";

            AuthenticateManager.AuthenState state = AuthenticateManager.Authenticate(context, iSabaya.SystemEnum.RiskAssessmentAdminSystem, idcard, firstname, ref u);

            //Act
            User uAct = context.PersistenceSession.Get <User>(1L);

            AuthenticateManager.AuthenState stateAct = AuthenticateManager.AuthenState.AuthenticationSuccess;

            //Assert
            Assert.AreEqual(state, stateAct);
            Assert.AreEqual(u, uAct);
        }
コード例 #3
0
        public void Authen_Test_Fail()
        {
            //Arrage
            User   u         = null;
            string idcard    = "0000000000000";
            string firstname = "test";

            AuthenticateManager.AuthenState state = AuthenticateManager.Authenticate(context, iSabaya.SystemEnum.RiskAssessmentAdminSystem, idcard, firstname, ref u);

            //Act
            User uAct = null;

            AuthenticateManager.AuthenState stateAct = AuthenticateManager.AuthenState.AuthenticationFail;

            //Assert
            Assert.AreEqual(state, stateAct);
            Assert.AreEqual(u, uAct);
        }
コード例 #4
0
 public IActionResult Login(
     [FromBody] UserDto userDto,
     [FromServices] AuthenticateManager authenticateManager,
     [FromServices] IUserServiceAuthenticate userService)
 {
     try
     {
         ObjectToken objectToken = authenticateManager.Authenticate(userDto, userService);
         if (objectToken.authenticated)
         {
             return(Ok(objectToken));
         }
         return(Unauthorized());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
コード例 #5
0
 public User Authenticate(User user)
 {
     return(authenticate.Authenticate(user));
 }
コード例 #6
0
        public string Authentication(string idCard, string nameEng, string status)
        {
            try
            {
                WebLogger.Warn("Authenticating");

                Dictionary <string, object> jsonResult = new Dictionary <string, object>();

                User user = null;

                switch (AuthenticateManager.Authenticate(SessionContext, SystemEnum.RiskAssessmentAnalysisSystem, idCard, nameEng, ref user))
                {
                case AuthenticateManager.AuthenState.AuthenticationSuccess:

                    BudgetConfiguration.CurrentConfiguration = GetConfiguration(SessionContext, SessionContext.MySystem.SystemID);
                    SessionContext.StartNewSession(user, Session.SessionID);

                    string targetPath = "";
                    switch (user.UserRoles[0].Role.Id)
                    {
                    case 1:
                        targetPath = FullUrl("Government");        //ส่วนราชการ
                        break;

                    case 2:
                        targetPath = FullUrl("Budgetor");        //ทำงบประมาณ
                        break;

                    case 3:
                        targetPath = FullUrl("Evaluation");        //เจ้าหน้าที่ประเมินงบ
                        break;

                    case 4:
                        targetPath = FullUrl("Admin");        //ผู้ดูแลระบบ
                        break;

                    default:
                        throw new Exception("User Role Invalid.");
                    }

                    jsonResult.Add("result", 1);
                    jsonResult.Add("target", targetPath);
                    jsonResult.Add("message", "");
                    SessionContext.Log(0, pageID, 0, MessageException.AuthenMessage.Login, MessageException.Success(SessionContext.User.ID.ToString()));
                    break;

                case AuthenticateManager.AuthenState.AuthenticationFail:
                    jsonResult.Add("result", 0);
                    jsonResult.Add("target", "");
                    jsonResult.Add("message", "Login Failed.");
                    SessionContext.Log(0, pageID, 0, MessageException.AuthenMessage.Login, MessageException.Fail(string.Format("{0} : {1}", idCard, nameEng)));
                    break;

                case AuthenticateManager.AuthenState.AlreadyLogin:
                    jsonResult.Add("result", 0);
                    jsonResult.Add("target", "");
                    jsonResult.Add("message", "Login Failed.");
                    SessionContext.Log(0, pageID, 0, MessageException.AuthenMessage.Login, MessageException.Fail(user.ID.ToString() + " : Login Attemp."));
                    break;

                default:
                    break;
                }

                WebLogger.Warn("End Authenticating");

                return(new JavaScriptSerializer().Serialize(jsonResult));

                #region old

                //Dictionary<string, object> jsonResult = new Dictionary<string, object>();
                //IList<SelfAuthenticatedUser> users = SessionContext.PersistenceSession.QueryOver<SelfAuthenticatedUser>().List();
                //IList<SelfAuthenticatedUser> user = users.Where(s => s.LoginName.ToLowerInvariant() == nameEng.ToLowerInvariant()
                //        && s.Person.OfficialIDNo == idCard
                //        && s.UserRoles[0].Role.Id == int.Parse(status)
                //        && !s.IsDisable
                //        && s.IsEffective).ToList();

                //if (0 < user.Count)
                //{
                //    if (user.Count != 1) { throw new Exception("User have more than one. System error."); }

                //    //if (!user[0].IsBuiltin)
                //    //{
                //    //    IList<iSabaya.UserSession> userSessions = SessionContext.PersistenceSession
                //    //        .QueryOver<iSabaya.UserSession>()
                //    //        .Where(us => us.User.ID == user[0].ID
                //    //            && us.SessionPeriod.To == iSabaya.TimeInterval.MaxDate)
                //    //        .List();

                //    //    if (userSessions.Any(u => u.User.ID == user[0].ID))
                //    //    {
                //    //        jsonResult.Add("result", 0);
                //    //        jsonResult.Add("target", "");
                //    //        jsonResult.Add("message", "Login Failed.");
                //    //        SessionContext.Log(0, pageID, 0, MessageException.AuthenMessage.Login, MessageException.Fail(userSessions[0].User.ID.ToString() + " : Login Attemp."));
                //    //        //SessionContext.StartFailedSession(null, idCard, Session.SessionID, "Login Attemp.");

                //    //        return new JavaScriptSerializer().Serialize(jsonResult);
                //    //    }
                //    //}

                //    BudgetConfiguration.CurrentConfiguration = GetConfiguration(SessionContext, SessionContext.MySystem.SystemID);
                //    SessionContext.StartNewSession(user[0], Session.SessionID);

                //    string targetPath = "";
                //    switch (user[0].UserRoles[0].Role.Id)
                //    {
                //        //case 1:
                //        //    targetPath = FullUrl("Government");//ส่วนราชการ
                //        //    break;
                //        case 2:
                //            targetPath = FullUrl("Budgetor");//ทำงบประมาณ
                //            break;
                //        case 3:
                //            targetPath = FullUrl("Evaluation");//เจ้าหน้าที่ประเมินงบ
                //            break;
                //        //case 4:
                //        //    targetPath = FullUrl("Admin");//ผู้ดูแลระบบ
                //        //    break;
                //        default:
                //            throw new Exception("User Role Invalid.");
                //    }

                //    jsonResult.Add("result", 1);
                //    jsonResult.Add("target", targetPath);
                //    jsonResult.Add("message", "");
                //    SessionContext.Log(0, pageID, 0, MessageException.AuthenMessage.Login, MessageException.Success(SessionContext.User.ID.ToString()));
                //}
                //else
                //{
                //    jsonResult.Add("result", 0);
                //    jsonResult.Add("target", "");
                //    jsonResult.Add("message", "Login Failed.");
                //    SessionContext.Log(0, pageID, 0, MessageException.AuthenMessage.Login, MessageException.Fail());
                //    //SessionContext.StartFailedSession(null, idCard, Session.SessionID, "Login Failed.");
                //}

                //WebLogger.Warn("End Authenticating");
                //return new JavaScriptSerializer().Serialize(jsonResult);

                #endregion old
            }
            catch (Exception ex)
            {
                WebLogger.Error(ex.GetAllMessages());

                Dictionary <string, object> jsonResult = new Dictionary <string, object>();
                jsonResult.Add("result", 0);
                jsonResult.Add("target", "");
                jsonResult.Add("message", ex.ToString());

                //SessionContext.StartFailedSession(null, idCard, Session.SessionID, ex.Message);

                SessionContext.Log(0, this.pageID, 0, MessageException.AuthenMessage.Login, MessageException.Fail(ex.Message));

                return(new JavaScriptSerializer().Serialize(jsonResult));
            }
        }
コード例 #7
0
        public string Authentication(string idCard, string nameEng, string status)
        {
            try
            {
                SessionContext.PersistenceSession.Clear();
                WebLogger.Warn("Authenticating");
                var  jsonResult = new Dictionary <string, object>();
                User user       = null;
                BudgetConfiguration.CurrentConfiguration
                    = GetConfiguration(SessionContext, SessionContext.MySystem.SystemID);

                switch (AuthenticateManager
                        .Authenticate(SessionContext,
                                      SystemEnum.RiskAssessmentProjectOwnerSystem,
                                      idCard,
                                      nameEng,
                                      ref user))
                {
                case AuthenticateManager.AuthenState.AuthenticationSuccess:

                    SessionContext.StartNewSession(user, Session.SessionID);

                    var targetPath = "";
                    switch (user.UserRoles[0].Role.Id)
                    {
                    case 1:
                        targetPath = FullUrl("Government");        //ส่วนราชการ
                        break;

                    case 2:
                        targetPath = FullUrl("Budgetor");        //ทำงบประมาณ
                        break;

                    case 3:
                        targetPath = FullUrl("Evaluation");        //เจ้าหน้าที่ประเมินงบ
                        break;

                    case 4:
                        targetPath = FullUrl("Admin");        //ผู้ดูแลระบบ
                        break;

                    default:
                        throw new Exception("User Role Invalid.");
                    }

                    jsonResult.Add("result", 1);
                    jsonResult.Add("target", targetPath);
                    jsonResult.Add("message", "");
                    SessionContext.Log(0, PageID, 0, MessageException.AuthenMessage.Login, MessageException.Success(SessionContext.User.ID.ToString()));
                    break;

                case AuthenticateManager.AuthenState.AuthenticationFail:
                    jsonResult.Add("result", 0);
                    jsonResult.Add("target", "");
                    jsonResult.Add("message", "Login Failed.");
                    SessionContext.Log(0, PageID, 0, MessageException.AuthenMessage.Login, MessageException.Fail(string.Format("{0} : {1}", idCard, nameEng)));
                    break;

                case AuthenticateManager.AuthenState.AlreadyLogin:
                    jsonResult.Add("result", 0);
                    jsonResult.Add("target", "");
                    jsonResult.Add("message", "Login Failed.");
                    SessionContext.Log(0, PageID, 0, MessageException.AuthenMessage.Login, MessageException.Fail(user.ID.ToString() + " : Login Attemp."));
                    break;

                default:
                    break;
                }

                WebLogger.Warn("End Authenticating");

                return(new JavaScriptSerializer().Serialize(jsonResult));
            }
            catch (Exception ex)
            {
                WebLogger.Error(ex.GetAllMessages());

                var jsonResult = new Dictionary <string, object>();
                jsonResult.Add("result", 0);
                jsonResult.Add("target", "");
                jsonResult.Add("message", ex.ToString());

                SessionContext.StartFailedSession(null, idCard, Session.SessionID, ex.Message);
                SessionContext.Log(0, this.PageID, 0, "Login", "Fail : " + ex.Message);

                return(new JavaScriptSerializer().Serialize(jsonResult));
            }
        }