source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/Manage/Index.cshtml.cs

main
Last change on this file was 2aea0fd, checked in by ElenaMoskova <elena.moskova99@…>, 4 months ago

init commit Elena

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
5using System;
6using System.ComponentModel.DataAnnotations;
7using System.Text.Encodings.Web;
8using System.Threading.Tasks;
9using Microsoft.AspNetCore.Identity;
10using Microsoft.AspNetCore.Mvc;
11using Microsoft.AspNetCore.Mvc.RazorPages;
12
13namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
14{
15 public class IndexModel : PageModel
16 {
17 private readonly UserManager<IdentityUser> _userManager;
18 private readonly SignInManager<IdentityUser> _signInManager;
19
20 public IndexModel(
21 UserManager<IdentityUser> userManager,
22 SignInManager<IdentityUser> signInManager)
23 {
24 _userManager = userManager;
25 _signInManager = signInManager;
26 }
27
28 /// <summary>
29 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
30 /// directly from your code. This API may change or be removed in future releases.
31 /// </summary>
32 public string Username { get; set; }
33
34 /// <summary>
35 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
36 /// directly from your code. This API may change or be removed in future releases.
37 /// </summary>
38 [TempData]
39 public string StatusMessage { get; set; }
40
41 /// <summary>
42 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
43 /// directly from your code. This API may change or be removed in future releases.
44 /// </summary>
45 [BindProperty]
46 public InputModel Input { get; set; }
47
48 /// <summary>
49 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
50 /// directly from your code. This API may change or be removed in future releases.
51 /// </summary>
52 public class InputModel
53 {
54 /// <summary>
55 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
56 /// directly from your code. This API may change or be removed in future releases.
57 /// </summary>
58 [Phone]
59 [Display(Name = "Phone number")]
60 public string PhoneNumber { get; set; }
61 }
62
63 private async Task LoadAsync(IdentityUser user)
64 {
65 var userName = await _userManager.GetUserNameAsync(user);
66 var phoneNumber = await _userManager.GetPhoneNumberAsync(user);
67
68 Username = userName;
69
70 Input = new InputModel
71 {
72 PhoneNumber = phoneNumber
73 };
74 }
75
76 public async Task<IActionResult> OnGetAsync()
77 {
78 var user = await _userManager.GetUserAsync(User);
79 if (user == null)
80 {
81 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
82 }
83
84 await LoadAsync(user);
85 return Page();
86 }
87
88 public async Task<IActionResult> OnPostAsync()
89 {
90 var user = await _userManager.GetUserAsync(User);
91 if (user == null)
92 {
93 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
94 }
95
96 if (!ModelState.IsValid)
97 {
98 await LoadAsync(user);
99 return Page();
100 }
101
102 var phoneNumber = await _userManager.GetPhoneNumberAsync(user);
103 if (Input.PhoneNumber != phoneNumber)
104 {
105 var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
106 if (!setPhoneResult.Succeeded)
107 {
108 StatusMessage = "Unexpected error when trying to set phone number.";
109 return RedirectToPage();
110 }
111 }
112
113 await _signInManager.RefreshSignInAsync(user);
114 StatusMessage = "Your profile has been updated";
115 return RedirectToPage();
116 }
117 }
118}
Note: See TracBrowser for help on using the repository browser.