private async Task <DomainUser> CompleteRegistrationAsync(TokenData tokenData) { DomainUser user = null; // Create new profile try { user = await _userService.AddAsync(CreateDomainUser(tokenData)); } catch (Exception e) { Trace.TraceError("Authentication failed for '{0}:{1}': {2}", tokenData.IdentityProvider, tokenData.UserIdentifier, e); throw; } // Send registration e-mail try { await _emailNotificationService.SendRegistrationEmailAsync(user); } catch (Exception e) { Trace.TraceError("Failed to send registration email to address {0} for user {1}: {2}", user.Email, user.Id, e); } // Send notification into social network ISocialNetworkNotifier notifier = _notificationFactory.GetNotifier(tokenData.IdentityProvider); if (notifier != null) { try { await notifier.SendWelcomeMessageAsync(user, tokenData); } catch (BadGatewayException) { } catch (Exception e) { Trace.TraceError("Failed to send welcome message to user '{0}': {1}", user.Id, e); } } //For statistics HttpContext.Items.Add("isRegister", true); return(user); }
public async Task <HttpResponseMessage> Post(SessionIpModel model) { ITokenDataExtractor tokenDataExtractor = _tokenDataExtractorFactory.CreateTokenDataExtractor(model.Type); TokenData tokenData; try { tokenData = tokenDataExtractor.Get(model); } catch (BadRequestException) { ModelState.AddModelError("Token", ResponseMessages.AccountsInvalidToken); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } // Return result DomainUser user = null; try { user = await _userService.FindByIdentityAsync(tokenData.IdentityProvider, tokenData.UserIdentifier); } catch (NotFoundException) { } // Try to find user by email if (user == null && !string.IsNullOrEmpty(tokenData.Email)) { try { user = await _userService.FindByEmailAsync(tokenData.Email); } catch (NotFoundException) { } // Add ip memebership to user if (user != null) { await _userService.AddMembersipAsync(user.Id, tokenData); } } var httpStatusCode = HttpStatusCode.OK; // Check whether registration is required if (user == null) { var profileData = new DomainUser { ApplicationName = AppName, Name = tokenData.Name, Email = tokenData.Email, UserAgent = _productIdExtractor.Get(UserAgent), Roles = new List <string> { DomainRoles.User }, Memberships = new List <UserMembership> { new UserMembership { IdentityProvider = tokenData.IdentityProvider, UserIdentifier = tokenData.UserIdentifier, } } }; user = await _userService.AddAsync(profileData); // Send registration e-mail try { await _emailNotificationService.SendRegistrationEmailAsync(user); } catch (Exception e) { Trace.TraceError("Failed to send registration email to address {0} for user {1}: {2}", user.Email, user.Id, e); } // Send notification into social network ISocialNetworkNotifier notifier = _notificationFactory.GetNotifier(tokenData.IdentityProvider); if (notifier != null) { try { await notifier.SendWelcomeMessageAsync(user, tokenData); } catch (BadGatewayException) { } catch (Exception e) { Trace.TraceError("Failed to send welcome message to user {0}: {1}", user.Id, e); } } httpStatusCode = HttpStatusCode.Created; } var token = await _authenticationService.SetUserAsync(user, tokenData, true); return(Request.CreateResponse(httpStatusCode, new AuthenticationTokenModel { Token = token })); }