1919using eShopOnContainers . Identity . Services ;
2020using eShopOnContainers . Identity . Models ;
2121using Microsoft . Extensions . Logging ;
22+ using Microsoft . AspNetCore . Authorization ;
23+ using eShopOnContainers . Identity . Models . AccountViewModels ;
24+ using Microsoft . AspNetCore . Identity ;
25+ using Microsoft . AspNetCore . Authentication ;
2226
2327namespace IdentityServer4 . Quickstart . UI . Controllers
2428{
@@ -34,19 +38,22 @@ public class AccountController : Controller
3438 private readonly IIdentityServerInteractionService _interaction ;
3539 private readonly IClientStore _clientStore ;
3640 private readonly ILogger _logger ;
41+ private readonly UserManager < ApplicationUser > _userManager ;
3742
3843 public AccountController (
3944
4045 //InMemoryUserLoginService loginService,
4146 ILoginService < ApplicationUser > loginService ,
4247 IIdentityServerInteractionService interaction ,
4348 IClientStore clientStore ,
44- ILoggerFactory loggerFactory )
49+ ILoggerFactory loggerFactory ,
50+ UserManager < ApplicationUser > userManager )
4551 {
4652 _loginService = loginService ;
4753 _interaction = interaction ;
4854 _clientStore = clientStore ;
4955 _logger = loggerFactory . CreateLogger < AccountController > ( ) ;
56+ _userManager = userManager ;
5057 }
5158
5259 /// <summary>
@@ -64,12 +71,6 @@ public async Task<IActionResult> Login(string returnUrl)
6471
6572 var vm = await BuildLoginViewModelAsync ( returnUrl , context ) ;
6673
67- if ( vm . EnableLocalLogin == false && vm . ExternalProviders . Count ( ) == 1 )
68- {
69- // only one option for logging in
70- return ExternalLogin ( vm . ExternalProviders . First ( ) . AuthenticationScheme , returnUrl ) ;
71- }
72-
7374 return View ( vm ) ;
7475 }
7576
@@ -78,11 +79,11 @@ public async Task<IActionResult> Login(string returnUrl)
7879 /// </summary>
7980 [ HttpPost ]
8081 [ ValidateAntiForgeryToken ]
81- public async Task < IActionResult > Login ( LoginInputModel model )
82+ public async Task < IActionResult > Login ( LoginViewModel model )
8283 {
8384 if ( ModelState . IsValid )
8485 {
85- var user = await _loginService . FindByUsername ( model . Username ) ;
86+ var user = await _loginService . FindByUsername ( model . Email ) ;
8687 // validate username/password against in-memory store
8788 if ( await _loginService . ValidateCredentials ( user , model . Password ) )
8889 {
@@ -92,7 +93,7 @@ public async Task<IActionResult> Login(LoginInputModel model)
9293 AuthenticationProperties props = null ;
9394 // only set explicit expiration here if persistent.
9495 // otherwise we reply upon expiration configured in cookie middleware.
95- if ( model . RememberLogin )
96+ if ( model . RememberMe )
9697 {
9798 props = new AuthenticationProperties
9899 {
@@ -101,7 +102,6 @@ public async Task<IActionResult> Login(LoginInputModel model)
101102 } ;
102103 } ;
103104
104- //await HttpContext.Authentication.SignInAsync(, user.UserName, props);
105105 await _loginService . SignIn ( user ) ;
106106
107107 // make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
@@ -123,44 +123,29 @@ public async Task<IActionResult> Login(LoginInputModel model)
123123
124124 async Task < LoginViewModel > BuildLoginViewModelAsync ( string returnUrl , AuthorizationRequest context )
125125 {
126- var providers = HttpContext . Authentication . GetAuthenticationSchemes ( )
127- . Where ( x => x . DisplayName != null )
128- . Select ( x => new ExternalProvider
129- {
130- DisplayName = x . DisplayName ,
131- AuthenticationScheme = x . AuthenticationScheme
132- } ) ;
133-
134126 var allowLocal = true ;
135127 if ( context ? . ClientId != null )
136128 {
137129 var client = await _clientStore . FindEnabledClientByIdAsync ( context . ClientId ) ;
138130 if ( client != null )
139131 {
140132 allowLocal = client . EnableLocalLogin ;
141-
142- if ( client . IdentityProviderRestrictions != null && client . IdentityProviderRestrictions . Any ( ) )
143- {
144- providers = providers . Where ( provider => client . IdentityProviderRestrictions . Contains ( provider . AuthenticationScheme ) ) ;
145- }
146133 }
147134 }
148135
149136 return new LoginViewModel
150137 {
151- EnableLocalLogin = allowLocal ,
152138 ReturnUrl = returnUrl ,
153- Username = context ? . LoginHint ,
154- ExternalProviders = providers . ToArray ( )
139+ Email = context ? . LoginHint ,
155140 } ;
156141 }
157142
158- async Task < LoginViewModel > BuildLoginViewModelAsync ( LoginInputModel model )
143+ async Task < LoginViewModel > BuildLoginViewModelAsync ( LoginViewModel model )
159144 {
160145 var context = await _interaction . GetAuthorizationContextAsync ( model . ReturnUrl ) ;
161146 var vm = await BuildLoginViewModelAsync ( model . ReturnUrl , context ) ;
162- vm . Username = model . Username ;
163- vm . RememberLogin = model . RememberLogin ;
147+ vm . Email = model . Email ;
148+ vm . RememberMe = model . RememberMe ;
164149 return vm ;
165150 }
166151
@@ -329,5 +314,62 @@ public async Task<IActionResult> ExternalLoginCallback(string returnUrl)
329314
330315 return Redirect ( "~/" ) ;
331316 }
317+
318+ // GET: /Account/Register
319+ [ HttpGet ]
320+ [ AllowAnonymous ]
321+ public IActionResult Register ( string returnUrl = null )
322+ {
323+ ViewData [ "ReturnUrl" ] = returnUrl ;
324+ return View ( ) ;
325+ }
326+
327+ //
328+ // POST: /Account/Register
329+ [ HttpPost ]
330+ [ AllowAnonymous ]
331+ [ ValidateAntiForgeryToken ]
332+ public async Task < IActionResult > Register ( RegisterViewModel model , string returnUrl = null )
333+ {
334+ ViewData [ "ReturnUrl" ] = returnUrl ;
335+ if ( ModelState . IsValid )
336+ {
337+ var user = new ApplicationUser
338+ {
339+ UserName = model . Email ,
340+ Email = model . Email ,
341+ CardHolderName = model . User . CardHolderName ,
342+ CardNumber = model . User . CardNumber ,
343+ CardType = model . User . CardType ,
344+ City = model . User . City ,
345+ Country = model . User . Country ,
346+ Expiration = model . User . Expiration ,
347+ LastName = model . User . LastName ,
348+ Name = model . User . Name ,
349+ Street = model . User . Street ,
350+ State = model . User . State ,
351+ ZipCode = model . User . ZipCode ,
352+ PhoneNumber = model . User . PhoneNumber ,
353+ SecurityNumber = model . User . SecurityNumber
354+ } ;
355+ var result = await _userManager . CreateAsync ( user , model . Password ) ;
356+ if ( result . Errors . Count ( ) > 0 )
357+ {
358+ AddErrors ( result ) ;
359+ // If we got this far, something failed, redisplay form
360+ return View ( model ) ;
361+ }
362+ }
363+
364+ return RedirectToAction ( "index" , "home" ) ;
365+ }
366+
367+ private void AddErrors ( IdentityResult result )
368+ {
369+ foreach ( var error in result . Errors )
370+ {
371+ ModelState . AddModelError ( string . Empty , error . Description ) ;
372+ }
373+ }
332374 }
333375}
0 commit comments