/// <summary> /// Registers the user. /// </summary> /// <param name="userModel">The user model.</param> /// <returns>Identity object</returns> public async Task<IdentityResult> RegisterUser(UserModel userModel) { ApplicationUser user = new ApplicationUser { Email = userModel.EmailId, UserName = userModel.EmailId, Name = userModel.Name }; var existingUser = this.userManager.FindByName(userModel.EmailId); if (existingUser != null) { var errorResult = new IdentityResult(new[] { string.Format("Email {0} already exists!", userModel.EmailId) }); return errorResult; } user.IsActive = true; if (userModel.UserRole == Roles.Company.ToString()) { user.AccountType = 3; } else if (userModel.UserRole == Roles.Customer.ToString()) { user.AccountType = 2; } var result = await this.userManager.CreateAsync(user, userModel.Password); if (result != null && result.Succeeded) { this.userManager.AddToRole(user.Id, userModel.UserRole); var newUser = this.mapperFactory.GetMapper<ApplicationUser, ApplicationUserDto>().Map(user); newUser.RoleName = userModel.UserRole; this.emailQueueService.SendActivationEmail(newUser); } return result; }
public async Task<IHttpActionResult> Register(UserModel userModel) { try { this.LoggerService.LogException("Register Request -json- " + JsonConvert.SerializeObject(userModel)); } catch (Exception ex1) { } if (string.IsNullOrEmpty(userModel.Country) && !string.IsNullOrEmpty(userModel.CountryCode)) { userModel.Country = this.commonService.GetCountryFromISO2Code(userModel.CountryCode); } if (string.IsNullOrEmpty(userModel.Country)) { userModel.Country = "United States"; } ////string ipAddress = string.IsNullOrEmpty(userModel.IPAddress) ? HttpContext.Current.Request.UserHostAddress : userModel.IPAddress; ////CityDto cityData = this.ip2LocationService.GetCityData(ipAddress); ////userModel.Country = cityData.CountryInfo.CountryName; ////userModel.State = cityData.MostSpecificSubDivisionName == null ? string.Empty : cityData.MostSpecificSubDivisionName; ////userModel.Latitude = cityData.Latitude; ////userModel.Longitude = cityData.Longitude; ////Only for handling old Android apps List<string> lstCallingCodes = this.commonService.GetCountryMetaData().Select(x => x.CallingCode).ToList(); try { IEnumerable<string> code = lstCallingCodes.Where(x => x == userModel.CountryCode); if (!string.IsNullOrEmpty(code.FirstOrDefault())) { userModel.CountryCode = this.commonService.GetISO2CodeFromCallingCode(code.FirstOrDefault()); } } catch { } StatusMessage status = new StatusMessage(); Roles userRole; bool isRoleParsed = Enum.TryParse(userModel.Role.ToString(), true, out userRole); if (!Roles.IsDefined(typeof(Roles), userModel.Role) || userRole == Roles.Admin) { return this.BadRequest(); } userModel.UserRole = userRole.ToString(); if (!this.ModelState.IsValid) { if (userModel.OSType != OSType.Web) { string errorMsg = this.ModelState.Values.First().Errors[0].ErrorMessage; return this.BadRequest(errorMsg); } return this.BadRequest(this.ModelState); } IdentityResult result; try { result = await this.authRepository.RegisterUser(userModel); if (!result.Succeeded) { string errorMessage = result.Errors.Any() ? result.Errors.First() : string.Empty; return this.BadRequest(errorMessage); } var user = await this.authRepository.FindUser(userModel.EmailId, userModel.Password); string defaultImageUrl = AppSettings.Get<string>(ConfigConstants.ApiBaseUrl) + AppSettings.Get<string>(ConfigConstants.DefaultProfileImage); string imageUrl = ImageHelper.SaveImageFromUrl(defaultImageUrl, user.Id); if (userModel.UserRole == Roles.Company.ToString()) { OrganisationModel orgModel = new OrganisationModel(); orgModel = this.mapperFactory.GetMapper<UserModel, OrganisationModel>().Map(userModel); orgModel.ImageURL = imageUrl; orgModel.IsImageUploaded = false; orgModel.CashBalance = orgModel.CreditBalance = 0; ////Only for handling old Android apps if (!string.IsNullOrEmpty(userModel.SubBusinessType) && userModel.SubBusinessType.Length > 0) { orgModel.SubBusinessType = userModel.SubBusinessType.Split(new string[] { "," }, StringSplitOptions.None).Where(x => !string.IsNullOrEmpty(x)).ToArray(); orgModel.MainBusinessType = this.youfferInterestService.GetMainBusinessTypeFromSub(userModel.SubBusinessType).Select(x => x.ParentBusinessTypeName).Distinct().ToArray(); } if (!string.IsNullOrEmpty(userModel.MainBusinessType) && userModel.MainBusinessType.Length > 0) { orgModel.MainBusinessType = userModel.MainBusinessType.Split(',').ToArray(); string[] subInterest = this.youfferInterestService.GetSubBusinessTypeFromMain(userModel.MainBusinessType).Select(x => x.BusinessTypeName).Distinct().ToArray(); orgModel.SubBusinessType = subInterest; } orgModel = this.crmManagerService.AddOrganisation(orgModel, user); ////Send username and password email await this.SendUserNamePasswordEmail(orgModel.Email1, orgModel.Password); } else { ContactModel contactModel = new ContactModel(); contactModel = this.mapperFactory.GetMapper<UserModel, ContactModel>().Map(userModel); contactModel.ImageURL = imageUrl; contactModel = this.crmManagerService.AddContact(contactModel, user); } } catch (Exception ex) { this.LoggerService.LogException("Register: " + ex.Message); this.ModelState.AddModelError("Exception", ex.Message); if (userModel.OSType != OSType.Web) { return this.BadRequest(ex.Message); } return this.BadRequest(this.ModelState); } IHttpActionResult errorResult = this.GetErrorResult(result); if (errorResult != null) { return errorResult; } status.IsSuccess = true; return this.Ok(status); }