public async Task <AuthorTestUser> RegisterUser(RegisterViewModel Input) { //create a new user var User = new AuthorTestUser { UserName = Input.Email, Email = Input.Email }; //add the user to our database var Result = await userManager.CreateAsync(User, Input.Password); if (Result.Succeeded) { var UserToAdd = await userManager.FindByEmailAsync(User.Email); var RoleExists = await roleManager.RoleExistsAsync("Contributor"); if (RoleExists) { await userManager.AddToRoleAsync(UserToAdd, "Contributor"); } else { var ContributorRole = new IdentityRole { Name = "Contributor" }; await roleManager.CreateAsync(ContributorRole); await userManager.AddToRoleAsync(UserToAdd, "Contributor"); } return(User); } return(null); }
//<-------------------------Account Management ------------------------> public async Task <AuthorTestUser> RegisterResearcher(ResearcherRegisterViewModel Input) { var User = new AuthorTestUser { UserName = Input.Email, Email = Input.Email, }; var CheckExists = await FindUserByEmail(User.Email); if (CheckExists == null) { //add the user to our database var result = await userManager.CreateAsync(User, Input.Password); //make sure researcher role exists if it doesn't create it. if (await roleManager.RoleExistsAsync("Researcher") != true) { var researcher = new IdentityRole { Name = "Researcher" }; await roleManager.CreateAsync(researcher); } await userManager.AddToRoleAsync(User, "Researcher"); var ResearcherInformation = await authorDbContext.ResearcherInfo.FirstOrDefaultAsync(v => v.AuthorTestUserId == User.Id); ResearcherInformation.ResearcherRole = "Pending Review"; ResearcherInformation.ResearchOrganization = Input.ResearchOrganization; ResearcherInformation.ResearchOrganizationAddress = Input.ResearchOrganizationAddress; ResearcherInformation.ResearchOrganizationEmail = Input.ResearchOrganizationEmail; authorDbContext.SaveChanges(); if (result.Succeeded) { // if user is added succesfully sign them in, using a non persistant cookie return(User); } } return(null); }
public async Task <bool> CreateUser(UserManager <AuthorTestUser> userManager, string email, string firstName, string role) { var check = await userManager.FindByEmailAsync(email); if (check == null) { var user = new AuthorTestUser { Email = email, UserName = email }; await userManager.CreateAsync(user, "password"); await userManager.AddToRoleAsync(user, role); } return(true); }