| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | #nullable disable |
| | | 4 | | |
| | | 5 | | using System.Text; |
| | | 6 | | using Microsoft.AspNetCore.Identity; |
| | | 7 | | using Microsoft.AspNetCore.Mvc; |
| | | 8 | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | 9 | | using Microsoft.AspNetCore.WebUtilities; |
| | | 10 | | using OzonRepositories.Context.Identity; |
| | | 11 | | |
| | | 12 | | namespace OzonOrdersWeb.Areas.Identity.Pages.Account; |
| | | 13 | | |
| | | 14 | | public class ConfirmEmailModel : PageModel |
| | | 15 | | { |
| | | 16 | | private readonly UserManager<CustomIdentityUser> _userManager; |
| | | 17 | | |
| | 0 | 18 | | public ConfirmEmailModel(UserManager<CustomIdentityUser> userManager) |
| | | 19 | | { |
| | 0 | 20 | | _userManager = userManager; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | [TempData] |
| | 0 | 24 | | public string StatusMessage { get; set; } |
| | | 25 | | |
| | | 26 | | public async Task<IActionResult> OnGetAsync(string userId, string code, string returnUrl = null) |
| | | 27 | | { |
| | 0 | 28 | | if (userId == null || code == null) |
| | | 29 | | { |
| | 0 | 30 | | return RedirectToPage("/Index"); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | var user = await _userManager.FindByIdAsync(userId); |
| | 0 | 34 | | if (user == null) |
| | | 35 | | { |
| | 0 | 36 | | return NotFound($"Не удалось загрузить пользователя с ID '{userId}'."); |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | var decodedCode = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code)); |
| | 0 | 40 | | var result = await _userManager.ConfirmEmailAsync(user, decodedCode); |
| | 0 | 41 | | StatusMessage = result.Succeeded |
| | 0 | 42 | | ? "Спасибо! Email подтвержден. Теперь ожидайте одобрения администратора." |
| | 0 | 43 | | : "Ошибка подтверждения email."; |
| | | 44 | | |
| | 0 | 45 | | if (!string.IsNullOrWhiteSpace(returnUrl)) |
| | | 46 | | { |
| | 0 | 47 | | return Redirect($"/Identity/Account/Login?ReturnUrl={Uri.EscapeDataString(returnUrl)}"); |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | return Page(); |
| | 0 | 51 | | } |
| | | 52 | | } |