22// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33
44
5+ using Identity . API . Models ;
6+ using Identity . API . Models . AccountViewModels ;
7+ using Identity . API . Services ;
58using IdentityModel ;
6- using IdentityServer4 . Quickstart . UI . Models ;
9+ using IdentityServer4 . Models ;
710using IdentityServer4 . Services ;
8- using Microsoft . AspNetCore . Http . Authentication ;
11+ using IdentityServer4 . Stores ;
12+ using Microsoft . AspNetCore . Authentication ;
13+ using Microsoft . AspNetCore . Authorization ;
14+ using Microsoft . AspNetCore . Identity ;
915using Microsoft . AspNetCore . Mvc ;
16+ using Microsoft . Extensions . Logging ;
1017using System ;
11- using System . Collections . Generic ;
1218using System . Linq ;
1319using System . Security . Claims ;
1420using System . Text . Encodings . Web ;
1521using System . Threading . Tasks ;
16- using IdentityServer4 . Models ;
17- using IdentityServer4 . Stores ;
18- using Identity . API . Services ;
19- using Identity . API . Models ;
20- using Microsoft . Extensions . Logging ;
21- using Microsoft . AspNetCore . Authorization ;
22- using Identity . API . Models . AccountViewModels ;
23- using Microsoft . AspNetCore . Identity ;
24- using Microsoft . AspNetCore . Authentication ;
2522
2623namespace IdentityServer4 . Quickstart . UI . Controllers
2724{
@@ -36,7 +33,7 @@ public class AccountController : Controller
3633 private readonly ILoginService < ApplicationUser > _loginService ;
3734 private readonly IIdentityServerInteractionService _interaction ;
3835 private readonly IClientStore _clientStore ;
39- private readonly ILogger _logger ;
36+ private readonly ILogger < AccountController > _logger ;
4037 private readonly UserManager < ApplicationUser > _userManager ;
4138
4239 public AccountController (
@@ -45,13 +42,13 @@ public AccountController(
4542 ILoginService < ApplicationUser > loginService ,
4643 IIdentityServerInteractionService interaction ,
4744 IClientStore clientStore ,
48- ILoggerFactory loggerFactory ,
45+ ILogger < AccountController > logger ,
4946 UserManager < ApplicationUser > userManager )
5047 {
5148 _loginService = loginService ;
5249 _interaction = interaction ;
5350 _clientStore = clientStore ;
54- _logger = loggerFactory . CreateLogger < AccountController > ( ) ;
51+ _logger = logger ;
5552 _userManager = userManager ;
5653 }
5754
@@ -69,6 +66,7 @@ public async Task<IActionResult> Login(string returnUrl)
6966 }
7067
7168 var vm = await BuildLoginViewModelAsync ( returnUrl , context ) ;
69+
7270 ViewData [ "ReturnUrl" ] = returnUrl ;
7371
7472 return View ( vm ) ;
@@ -97,6 +95,7 @@ public async Task<IActionResult> Login(LoginViewModel model)
9795 } ;
9896
9997 await _loginService . SignIn ( user ) ;
98+
10099 // make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
101100 if ( _interaction . IsValidReturnUrl ( model . ReturnUrl ) )
102101 {
@@ -111,7 +110,9 @@ public async Task<IActionResult> Login(LoginViewModel model)
111110
112111 // something went wrong, show form with error
113112 var vm = await BuildLoginViewModelAsync ( model ) ;
113+
114114 ViewData [ "ReturnUrl" ] = model . ReturnUrl ;
115+
115116 return View ( vm ) ;
116117 }
117118
@@ -180,6 +181,7 @@ public async Task<IActionResult> Logout(string logoutId)
180181 public async Task < IActionResult > Logout ( LogoutViewModel model )
181182 {
182183 var idp = User ? . FindFirst ( JwtClaimTypes . IdentityProvider ) ? . Value ;
184+
183185 if ( idp != null && idp != IdentityServerConstants . LocalIdentityProvider )
184186 {
185187 if ( model . LogoutId == null )
@@ -191,10 +193,15 @@ public async Task<IActionResult> Logout(LogoutViewModel model)
191193 }
192194
193195 string url = "/Account/Logout?logoutId=" + model . LogoutId ;
196+
194197 try
195198 {
199+
196200 // hack: try/catch to handle social providers that throw
197- await HttpContext . Authentication . SignOutAsync ( idp , new AuthenticationProperties { RedirectUri = url } ) ;
201+ await HttpContext . SignOutAsync ( idp , new AuthenticationProperties
202+ {
203+ RedirectUri = url
204+ } ) ;
198205 }
199206 catch ( Exception ex )
200207 {
@@ -203,7 +210,7 @@ public async Task<IActionResult> Logout(LogoutViewModel model)
203210 }
204211
205212 // delete authentication cookie
206- await HttpContext . Authentication . SignOutAsync ( ) ;
213+ await HttpContext . SignOutAsync ( ) ;
207214
208215 // set this so UI rendering sees an anonymous user
209216 HttpContext . User = new ClaimsPrincipal ( new ClaimsIdentity ( ) ) ;
@@ -217,7 +224,7 @@ public async Task<IActionResult> Logout(LogoutViewModel model)
217224 public async Task < IActionResult > DeviceLogOut ( string redirectUrl )
218225 {
219226 // delete authentication cookie
220- await HttpContext . Authentication . SignOutAsync ( ) ;
227+ await HttpContext . SignOutAsync ( ) ;
221228
222229 // set this so UI rendering sees an anonymous user
223230 HttpContext . User = new ClaimsPrincipal ( new ClaimsIdentity ( ) ) ;
0 commit comments