public async Task <StatusBuilder> Register([FromBody] RegistrationDetails model) { var results = this.HandleModelState(); if (results.IsSuccessful) { results = await _accountService.RegisterAsync(model); } return(results); }
public async Task <StatusBuilder> RegisterAsync(RegistrationDetails details) { var output = new StatusBuilder(); var user = await _userManager.FindByNameAsync(details.Username); if (user == null) { user = new ApplicationUser { Id = Guid.NewGuid().ToString(), UserName = details.Username, Email = details.Email }; output.SetData(user); var identityResult = await _userManager.CreateAsync(user, details.Password); if (identityResult.Succeeded) { if (details.StartFreeTrial) { var trialClaim = new Claim("Trial", DateTime.Now.ToString()); await _userManager.AddClaimAsync(user, trialClaim); } else if (details.IsAdmin) { await _userManager.AddToRoleAsync(user, "Admin"); } } else { foreach (var err in identityResult.Errors) { output.RecordFailure(err.Description, err.Code); } } } else { output.RecordFailure("User already exists"); } return(output); }