public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _repo.FindUserAsync(new UserLoginInfo(model.Provider, model.UserId)); var hasRegistered = user != null; if (hasRegistered) { return(BadRequest("External user is already registered")); } user = new ApplicationUser() { UserName = model.UserName, Id = Guid.NewGuid().ToString() }; var result = await _repo.CreateUserAsync(user); if (!result.Succeeded) { return(GetErrorResult(result)); } var info = new ExternalLoginInfo() { DefaultUserName = model.UserName, Login = new UserLoginInfo(model.Provider, model.UserId) }; result = await _repo.AddUserLoginAsync(user.Id, info.Login); if (!result.Succeeded) { return(GetErrorResult(result)); } //generate access token response var accessTokenResponse = _authService.GenerateLocalAccessTokenResponse(user.UserName, user.Id, Startup.OAuthBearerOptions); return(Ok(accessTokenResponse)); }