public async Task <ActionResult <ReturnSignUserResponse> > PostAsync( [FromBody] RegisterRequest model, ReturnUrlRequest?returnUrl, CancellationToken token) { var user = await _userManager.FindByEmailAsync(model.Email); if (user != null) { try { return(await MakeDecisionAsync(user, false, returnUrl, token)); } catch (ArgumentException) { } ModelState.AddModelError(nameof(model.Email), _localizer["UserExists"]); return(BadRequest(ModelState)); } var country = await _countryProvider.GetUserCountryAsync(token); user = new User(model.Email, model.FirstName, model.LastName, CultureInfo.CurrentCulture, country, model.Gender); var p = await _userManager.CreateAsync(user, model.Password); if (p.Succeeded) { await GenerateEmailAsync(user, returnUrl, token); return(new ReturnSignUserResponse(RegistrationStep.RegisterEmailConfirmed)); } ModelState.AddIdentityModelError(p); return(BadRequest(ModelState)); }
public async Task <IActionResult> VerifySmsAsync( [FromBody] CodeRequest model, [ModelBinder(typeof(CountryModelBinder))] string country, [FromHeader(Name = "User-Agent")] string agent, CancellationToken token) { var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { _logger.Error("VerifySmsAsync We can't identify the user"); return(Unauthorized()); } var v = await _userManager.ChangePhoneNumberAsync(user, user.PhoneNumber, model.Number); if (v.Succeeded) { agent = agent.Substring(0, Math.Min(agent.Length, 255)); return(await FinishRegistrationAsync(user, country, model.FingerPrint, agent, token)); } _logger.Warning($"userid: {user.Id} is not verified reason: {v}"); ModelState.AddIdentityModelError(v); return(BadRequest(ModelState)); }
public async Task <IActionResult> ResetPasswordAsync([FromBody] ResetPasswordRequest model, [FromHeader(Name = "referer")] Uri referer, CancellationToken token) { var queryString = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(referer.Query); if (queryString.TryGetValue("id", out var id) && queryString.TryGetValue("code", out var code) ) { // var id = queryString["id"]; //var code = queryString["code"]; //var from = queryString["byPass"]; if (id == StringValues.Empty || code == StringValues.Empty) { return(BadRequest()); } var user = await _userManager.FindByIdAsync(id); if (user == null) { // Don't reveal that the user does not exist return(BadRequest()); } user.EmailConfirmed = true; code = System.Net.WebUtility.UrlDecode(code); var result = await _userManager.ResetPasswordAsync(user, code, model.Password); if (result.Succeeded) { //if (from != null) //{ // await _signInManager.SignInAsync(user, false); // return Ok(); //} if (user.PhoneNumberConfirmed) { await _signInManager.SignInAsync(user, false); return(Ok()); } return(Ok()); //ModelState.AddModelError(nameof(model.Password), _localizer["UserDoesntExists"]); //return BadRequest(ModelState); } //TODO: Localize ModelState.AddIdentityModelError(nameof(model.Password), result); return(BadRequest(ModelState)); } return(BadRequest()); }
public async Task <IActionResult> SetUserPhoneNumberAsync( [FromBody] PhoneNumberRequest model, CancellationToken token) { if (User.Identity.IsAuthenticated) { _logger.Error("Set User Phone number User is already sign in"); return(Unauthorized()); } var user = await _signInManager.GetTwoFactorAuthenticationUserAsync(); if (user == null) { _logger.Error("Set User Phone number We can't identify the user"); return(Unauthorized()); } if (!user.EmailConfirmed || user.PhoneNumberConfirmed) { return(Unauthorized()); } var retVal = await _userManager.SetPhoneNumberAndCountryAsync(user, model.PhoneNumber, model.CountryCode.ToString(), token); //Ram: I disable this - we have an issue that sometime we get the wrong ip look at id //3DCDBF98-6545-473A-8EAA-A9DF00787C70 of UserLocation table in dev sql //if (country != null) //{ // if (!string.Equals(user.Country, country, StringComparison.OrdinalIgnoreCase)) // { // var command2 = new AddUserLocationCommand(user, country, HttpContext.Connection.GetIpAddress()); // var t1 = _commandBus.DispatchAsync(command2, token); // await _userManager.SetLockoutEndDateAsync(user, DateTimeOffset.MaxValue); // ModelState.AddModelError(nameof(model.PhoneNumber), _smsLocalizer["PhoneNumberNotSameCountry"]); // var t2 = _signInManager.SignOutAsync(); // await Task.WhenAll(t1, t2); // return BadRequest(ModelState); // } //} if (retVal.Succeeded) { TempData[SmsTime] = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture); TempData[PhoneCallTime] = DateTime.UtcNow.AddMinutes(-2).ToString(CultureInfo.InvariantCulture); await _client.SendSmsAsync(user, token); return(Ok()); } if (retVal.Errors.Any(a => a.Code == "InvalidPhoneNumber")) { ModelState.AddModelError(nameof(model.PhoneNumber), _localizer["InvalidPhoneNumber"]); } if (retVal.Errors.Any(a => a.Code == "Duplicate")) { _logger.Warning("phone number is duplicate"); ModelState.AddModelError(nameof(model.PhoneNumber), _smsLocalizer["DuplicatePhoneNumber"]); } else { _logger.Warning("Some other error" + retVal.Errors.FirstOrDefault()?.Description); ModelState.AddIdentityModelError(retVal); } return(BadRequest(ModelState)); }