public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin == null) { return InternalServerError(); } var user = new UserProfile{ UserName = model.UserName, Email = model.Email, EmailConfirmed = true }; user.Logins.Add(new IdentityUserLogin{ LoginProvider = externalLogin.LoginProvider, ProviderKey = externalLogin.ProviderKey, UserId = user.Id }); IdentityResult identityResult = await UserManager.CreateAsync(user); IHttpActionResult createResult = GetErrorResult(identityResult); if (createResult != null) { return createResult; } UserProfile justCreatedUser = await UserManager.FindByNameAsync(model.UserName); IdentityResult roleResult = await UserManager.AddToRoleAsync(justCreatedUser.Id, "User"); IHttpActionResult addRoleResult = GetErrorResult(roleResult); if (addRoleResult != null) { return addRoleResult; } return Ok(); }
public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var user = new UserProfile { UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber, Email = model.Email, EmailConfirmed = false, }; if (model.NetworkId != null) { Guid networkId = Guid.Parse(model.NetworkId); Network network = _unitOfWork.NetworkRepository.First(n => n.NetworkId == networkId, "NetworkCompanies"); NetworkCompany nc = CreateCompany(model, network); network.NetworkCompanies.Add(nc); user.CompanyId = nc.Company.CompanyId; } else if (model.CompanyId != null) { user.CompanyId = Guid.Parse(model.CompanyId); } _unitOfWork.Commit(); IdentityResult identityResult = await UserManager.CreateAsync(user, model.Password); if (identityResult.Succeeded && (model.NetworkId != null || model.CompanyId != null)) { UserManager.AddToRole(user.Id, "User"); } IHttpActionResult createResult = GetErrorResult(identityResult); if (createResult != null) { return createResult; } UserProfile justCreatedUser = await UserManager.FindByNameAsync(model.UserName); string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); string callbackUrl = Url.Link("ConfirmEmail", new{ userId = user.Id, code }); var notification = new AccountNotificationModel{ Code = code, Url = callbackUrl, UserId = justCreatedUser.Id, Email = justCreatedUser.Email, DisplayName = justCreatedUser.UserName }; string body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml", notification); await UserManager.SendEmailAsync(user.Id, "EnergieNetz: Konto aktivieren", body); DeleteInvitation(model); return Ok(); }