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"); } }
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); } }