public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value; var user = await _userManager.FindByIdAsync(userId); if (user == null) { return(NotFound($"Unable to load user with ID '{userId}'.")); } var result = await _userManager.ConfirmSignUpAsync(user, Input.Code, true); if (!result.Succeeded) { throw new InvalidOperationException($"Error confirming account for user with ID '{userId}':"); } else { return(returnUrl != null?LocalRedirect(returnUrl) : Page() as IActionResult); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <ICommunicationClient> CreateCommunicationClient(CancellationToken token) { var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); var user = authState.User; if (!user.Identity.IsAuthenticated) { throw new Exception(); } var userId = _cognitoUserManager.GetUserId(user); if (string.IsNullOrEmpty(userId)) { throw new Exception(); } var cognitoUser = await _cognitoUserManager.FindByIdAsync(userId); if (string.IsNullOrEmpty(cognitoUser?.SessionTokens.IdToken)) { throw new Exception(); } var cws = new ClientWebSocket(); cws.Options.SetRequestHeader("Authorization", cognitoUser.SessionTokens.IdToken); await cws.ConnectAsync(new Uri(_appOptions.ImageRecognitionWebSocketAPI), token); return(new CommunicationClient(cws)); }
private async Task <HttpClient> ConstructHttpClient() { var authState = await _authenticationStateProvider.GetAuthenticationStateAsync(); var user = authState.User; if (!user.Identity.IsAuthenticated) { throw new Exception(); } var userId = _cognitoUserManager.GetUserId(user); if (string.IsNullOrEmpty(userId)) { throw new Exception(); } var cognitoUser = await _cognitoUserManager.FindByIdAsync(userId); if (string.IsNullOrEmpty(cognitoUser?.SessionTokens.IdToken)) { throw new Exception(); } var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse($"bearer {cognitoUser.SessionTokens.IdToken}"); return(httpClient); }
public async Task <ActionResult> RemoveLogin(string loginProvider, string providerKey) { ManageMessageId?message; var result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey)); if (result.Succeeded) { var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); if (user != null) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); } message = ManageMessageId.RemoveLoginSuccess; } else { message = ManageMessageId.Error; } return(RedirectToAction("ManageLogins", new { Message = message })); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { //get user id var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value; var user = await _userManager.FindByIdAsync(userId); if (user == null) { return(NotFound($"Unable to load user with ID '{userId}'.")); } var result = await _userManager.ConfirmSignUpAsync(user, Input.Code, true); if (!result.Succeeded) { throw new InvalidOperationException($"Error confirming account for user with ID '{userId}':"); } else { //this part is to add customer information into the DB var userName = await _userManager.GetUserNameAsync(user); var firstName = user.Attributes[CognitoAttribute.GivenName.AttributeName]; var lastName = user.Attributes[CognitoAttribute.FamilyName.AttributeName]; var email = user.Attributes[CognitoAttribute.Email.AttributeName]; var dateOfBirth = user.Attributes[CognitoAttribute.BirthDate.AttributeName]; var phone = user.Attributes[CognitoAttribute.PhoneNumber.AttributeName]; var customer_ID = user.Attributes[CognitoAttribute.Sub.AttributeName]; var customer = new Customer() { Customer_Id = customer_ID, //Customer_Id = 1111, Username = userName, FirstName = firstName, LastName = lastName, Email = email, Phone = phone }; _context.Add(customer); _context.SaveChanges(); var currentCustomerID = _context.Customer.Find(user.Attributes[CognitoAttribute.Sub.AttributeName]); //get cardid var cartId = HttpContext.Request.Cookies["CartId"]; var recentCart = await _context.Cart.FindAsync(Convert.ToString(cartId)); recentCart.Customer = currentCustomerID; _context.Update(recentCart); await _context.SaveChangesAsync(); return(returnUrl != null?LocalRedirect(returnUrl) : Page() as IActionResult); } } // If we got this far, something failed, redisplay form return(Page()); }