Esempio n. 1
0
        private async void CreateUserAsync()
        {
            //RoleManager<IdentityRole> roleManager1 = ApplicationIdentityManager.GetRoleManager();
            //IdentityRole role1 = new IdentityRole("SuperAdmin");
            //IdentityResult result2 = await roleManager1.CreateAsync(role1);

            //role1 = new IdentityRole("CaseOfficer");
            //result2 = await roleManager1.CreateAsync(role1);

            //role1 = new IdentityRole("EnrolmentOfficer");
            //result2 = await roleManager1.CreateAsync(role1);

            //role1 = new IdentityRole("Supervisee");
            //result2 = await roleManager1.CreateAsync(role1);
            //MessageBox.Show("Ngon");
            //return;
            UserManager <ApplicationUser> userManager = ApplicationIdentityManager.GetUserManager();
            ApplicationUser user = new ApplicationUser()
            {
                UserName = "******"
            };

            IdentityResult result = await userManager.CreateAsync(user, "123456");

            if (result.Succeeded)
            {
                RoleManager <IdentityRole> roleManager = ApplicationIdentityManager.GetRoleManager();
                userManager.AddToRole(user.Id, "Supervisee");
            }
            else
            {
                MessageBox.Show("Hỏng hết cơm cháo");
            }
        }
Esempio n. 2
0
        public void ManualLogin(string username, string password)
        {
            EventCenter eventCenter = EventCenter.Default;

            UserManager <ApplicationUser> userManager = ApplicationIdentityManager.GetUserManager();
            ApplicationUser appUser = userManager.Find(username, password);

            if (appUser != null)
            {
                // Authenticated successfully
                // Check if the current user is an Duty Officer or not
                if (userManager.IsInRole(appUser.Id, EnumUserRoles.DutyOfficer))
                {
                    // Authorized successfully
                    Trinity.BE.User user = new Trinity.BE.User()
                    {
                        RightThumbFingerprint = appUser.RightThumbFingerprint,
                        LeftThumbFingerprint  = appUser.LeftThumbFingerprint,
                        IsFirstAttempt        = appUser.IsFirstAttempt,
                        Name        = appUser.Name,
                        NRIC        = appUser.NRIC,
                        Role        = EnumUserRoles.DutyOfficer,
                        SmartCardId = appUser.SmartCardId,
                        Status      = appUser.Status,
                        UserId      = appUser.Id
                    };
                    Session session = Session.Instance;
                    session.IsUserNamePasswordAuthenticated = true;
                    session.Role = EnumUserRoles.DutyOfficer;
                    session[CommonConstants.USER_LOGIN] = user;

                    eventCenter.RaiseEvent(new Trinity.Common.EventInfo()
                    {
                        Code = 0, Name = EventNames.LOGIN_SUCCEEDED
                    });
                }
                else
                {
                    eventCenter.RaiseEvent(new Trinity.Common.EventInfo()
                    {
                        Code = -2, Name = EventNames.LOGIN_FAILED, Message = "You do not have permission to access this page."
                    });
                }
            }
            else
            {
                eventCenter.RaiseEvent(new Trinity.Common.EventInfo()
                {
                    Code = -1, Name = EventNames.LOGIN_FAILED, Message = "Your username or password is incorrect."
                });
            }
        }
Esempio n. 3
0
 public ApplicationUser FindByName(string username)
 {
     if (EnumAppConfig.IsLocal)
     {
         ApplicationUser user = ApplicationIdentityManager.GetUserManager().FindByName(username);
         //if (user == null)
         //{
         //    user = CallCentralized.Get<ApplicationUser>("User", "FindByName", "username=" + username);
         //}
         return(user);
     }
     else
     {
         return(ApplicationIdentityManager.GetUserManager().FindByName(username));
     }
 }
Esempio n. 4
0
 public bool IsInRole(string Id, string Role)
 {
     if (EnumAppConfig.IsLocal)
     {
         bool?status = status = ApplicationIdentityManager.GetUserManager().IsInRole(Id, Role);
         //if (status == null)
         //{
         //    status = CallCentralized.Get<bool>("User", "IsInRole", "Id=" + Id, "Role=" + Role);
         //}
         return(status.Value);
     }
     else
     {
         return(ApplicationIdentityManager.GetUserManager().IsInRole(Id, Role));;
     }
 }
Esempio n. 5
0
 public ApplicationUser Login(string username, string password)
 {
     if (EnumAppConfig.IsLocal)
     {
         ApplicationUser user = user = ApplicationIdentityManager.GetUserManager().Find(username, password);
         //if (user == null)
         //{
         //    user = CallCentralized.Get<ApplicationUser>("User", "Login", "username="******"password=" + password);
         //}
         return(user);
     }
     else
     {
         return(null);
         //return ApplicationIdentityManager.GetUserManager().Find(username, password);
     }
 }
Esempio n. 6
0
        public BE.User Login_Centralized(string username, string password)
        {
            if (EnumAppConfig.IsLocal)
            {
                return(null);
            }

            //password = CreatePasswordHash(password);

            ApplicationUser applicationUser = ApplicationIdentityManager.GetUserManager().Find(username, password);

            if (applicationUser != null)
            {
                // get data
                BE.User returnValue = _centralizedUnitOfWork.DataContext.Membership_Users
                                      .Where(item => item.NRIC == applicationUser.NRIC)
                                      .Select(item => new Trinity.BE.User()
                {
                    UserId                = item.UserId,
                    Status                = item.Status,
                    SmartCardId           = item.SmartCardId,
                    RightThumbFingerprint =
                        item.RightThumbFingerprint,
                    LeftThumbFingerprint = item.LeftThumbFingerprint,
                    Name           = item.Name,
                    NRIC           = item.NRIC,
                    Role           = item.Membership_UserRoles.FirstOrDefault().Membership_Roles.Name,
                    IsFirstAttempt = item.IsFirstAttempt
                }).FirstOrDefault();

                // return value
                return(returnValue);
            }

            return(null);
        }
Esempio n. 7
0
        private async void CreateUserAsync()
        {
            //if (string.IsNullOrEmpty(_currentUser.SmartCardId) || _currentUser.Fingerprint == null)
            //{
            //    MessageBox.Show("You have to scan your smart card and fingerprint");
            //    return;
            //}
            //
            // Prepare user information
            //

            _currentUser.Name = txtName.Text;
            _currentUser.NRIC = txtNRIC.Text;
            _currentUser.Role = String.IsNullOrEmpty(cboRoles.Text) ? EnumUserRoles.Supervisee : cboRoles.Text;

            ApplicationUser user = new ApplicationUser();

            user.UserName = _currentUser.NRIC;
            user.Name     = _currentUser.Name;
            user.Email    = txtPrimaryEmail.Text;
            user.RightThumbFingerprint = _currentUser.RightThumbFingerprint;
            user.LeftThumbFingerprint  = _currentUser.LeftThumbFingerprint;
            user.IsFirstAttempt        = _currentUser.IsFirstAttempt;
            user.NRIC        = _currentUser.NRIC;
            user.PhoneNumber = txtPrimaryPhone.Text;
            user.SmartCardId = _currentUser.SmartCardId;
            user.Status      = EnumUserStatuses.Enrolled;

            UserManager <ApplicationUser> userManager = ApplicationIdentityManager.GetUserManager();

            Trinity.DAL.DAL_User dalUser = new Trinity.DAL.DAL_User();
            IdentityResult       result  = await userManager.CreateAsync(user, txtPassword.Text.Trim());

            if (result.Succeeded)
            {
                RoleManager <IdentityRole> roleManager = ApplicationIdentityManager.GetRoleManager();
                userManager.AddToRole(user.Id, _currentUser.Role);
                // Save to the Centralized DB also
                //dalUser.CreateUser(_currentUser, false);

                Trinity.DAL.DAL_UserProfile dalUserProfile = new Trinity.DAL.DAL_UserProfile();
                Trinity.BE.UserProfile      userProfile    = new Trinity.BE.UserProfile();
                userProfile.UserId        = _currentUser.UserId;
                userProfile.Primary_Phone = txtPrimaryPhone.Text;
                userProfile.Primary_Email = txtPrimaryEmail.Text;
                userProfile.Nationality   = txtNationality.Text;
                userProfile.DOB           = dpDOB.Value;



                var updateUProfileResult = CallCentralized.Post <bool>("User", "UpdateUserProfile", userProfile);

                //dalUserProfile.UpdateUserProfile(userProfile, _currentUser.UserId, true);

                //// Save to the Centralized DB also
                //dalUserProfile.UpdateUserProfile(userProfile, _currentUser.UserId, false);

                Trinity.BE.IssueCard issuedCard = new Trinity.BE.IssueCard()
                {
                    CreatedDate   = DateTime.Now,
                    Date_Of_Issue = DateTime.Now,
                    Expired_Date  = DateTime.Now.AddYears(2),
                    Name          = _currentUser.Name,
                    NRIC          = _currentUser.NRIC,
                    Serial_Number = "123434",
                    SmartCardId   = _currentUser.SmartCardId,
                    Status        = "Active",
                    UserId        = user.Id
                };
                DAL_IssueCard dalIssuedCard = new DAL_IssueCard();
                dalIssuedCard.Insert(issuedCard);

                btnSave.Enabled = false;
                MessageBox.Show("Create user successfully!", "Create user", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Form frmMain = (Form)this.MainForm;
                frmMain.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("Could not create user.", "Create user", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }