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

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

init commit Elena

  • Property mode set to 100644
File size: 5.8 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.Collections.Generic;
7using System.Linq;
8using System.Threading;
9using System.Threading.Tasks;
10using Microsoft.AspNetCore.Authentication;
11using Microsoft.AspNetCore.Identity;
12using Microsoft.AspNetCore.Mvc;
13using Microsoft.AspNetCore.Mvc.RazorPages;
14
15namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
16{
17 public class ExternalLoginsModel : PageModel
18 {
19 private readonly UserManager<IdentityUser> _userManager;
20 private readonly SignInManager<IdentityUser> _signInManager;
21 private readonly IUserStore<IdentityUser> _userStore;
22
23 public ExternalLoginsModel(
24 UserManager<IdentityUser> userManager,
25 SignInManager<IdentityUser> signInManager,
26 IUserStore<IdentityUser> userStore)
27 {
28 _userManager = userManager;
29 _signInManager = signInManager;
30 _userStore = userStore;
31 }
32
33 /// <summary>
34 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
35 /// directly from your code. This API may change or be removed in future releases.
36 /// </summary>
37 public IList<UserLoginInfo> CurrentLogins { get; set; }
38
39 /// <summary>
40 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
41 /// directly from your code. This API may change or be removed in future releases.
42 /// </summary>
43 public IList<AuthenticationScheme> OtherLogins { get; set; }
44
45 /// <summary>
46 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
47 /// directly from your code. This API may change or be removed in future releases.
48 /// </summary>
49 public bool ShowRemoveButton { get; set; }
50
51 /// <summary>
52 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
53 /// directly from your code. This API may change or be removed in future releases.
54 /// </summary>
55 [TempData]
56 public string StatusMessage { get; set; }
57
58 public async Task<IActionResult> OnGetAsync()
59 {
60 var user = await _userManager.GetUserAsync(User);
61 if (user == null)
62 {
63 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
64 }
65
66 CurrentLogins = await _userManager.GetLoginsAsync(user);
67 OtherLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
68 .Where(auth => CurrentLogins.All(ul => auth.Name != ul.LoginProvider))
69 .ToList();
70
71 string passwordHash = null;
72 if (_userStore is IUserPasswordStore<IdentityUser> userPasswordStore)
73 {
74 passwordHash = await userPasswordStore.GetPasswordHashAsync(user, HttpContext.RequestAborted);
75 }
76
77 ShowRemoveButton = passwordHash != null || CurrentLogins.Count > 1;
78 return Page();
79 }
80
81 public async Task<IActionResult> OnPostRemoveLoginAsync(string loginProvider, string providerKey)
82 {
83 var user = await _userManager.GetUserAsync(User);
84 if (user == null)
85 {
86 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
87 }
88
89 var result = await _userManager.RemoveLoginAsync(user, loginProvider, providerKey);
90 if (!result.Succeeded)
91 {
92 StatusMessage = "The external login was not removed.";
93 return RedirectToPage();
94 }
95
96 await _signInManager.RefreshSignInAsync(user);
97 StatusMessage = "The external login was removed.";
98 return RedirectToPage();
99 }
100
101 public async Task<IActionResult> OnPostLinkLoginAsync(string provider)
102 {
103 // Clear the existing external cookie to ensure a clean login process
104 await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
105
106 // Request a redirect to the external login provider to link a login for the current user
107 var redirectUrl = Url.Page("./ExternalLogins", pageHandler: "LinkLoginCallback");
108 var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, _userManager.GetUserId(User));
109 return new ChallengeResult(provider, properties);
110 }
111
112 public async Task<IActionResult> OnGetLinkLoginCallbackAsync()
113 {
114 var user = await _userManager.GetUserAsync(User);
115 if (user == null)
116 {
117 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
118 }
119
120 var userId = await _userManager.GetUserIdAsync(user);
121 var info = await _signInManager.GetExternalLoginInfoAsync(userId);
122 if (info == null)
123 {
124 throw new InvalidOperationException($"Unexpected error occurred loading external login info.");
125 }
126
127 var result = await _userManager.AddLoginAsync(user, info);
128 if (!result.Succeeded)
129 {
130 StatusMessage = "The external login was not added. External logins can only be associated with one account.";
131 return RedirectToPage();
132 }
133
134 // Clear the existing external cookie to ensure a clean login process
135 await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
136
137 StatusMessage = "The external login was added.";
138 return RedirectToPage();
139 }
140 }
141}
Note: See TracBrowser for help on using the repository browser.