17
17
using Abp . Timing ;
18
18
using Abp . UI ;
19
19
using Abp . Web . Models ;
20
- using Abp . Zero . AspNetCore ;
21
20
using Abp . Zero . Configuration ;
22
21
using Microsoft . AspNetCore . Mvc ;
23
22
using AbpCompanyName . AbpProjectName . Authorization ;
24
23
using AbpCompanyName . AbpProjectName . MultiTenancy ;
25
24
using AbpCompanyName . AbpProjectName . Web . Models . Account ;
26
25
using AbpCompanyName . AbpProjectName . Authorization . Users ;
27
26
using AbpCompanyName . AbpProjectName . Controllers ;
27
+ using AbpCompanyName . AbpProjectName . Identity ;
28
28
using AbpCompanyName . AbpProjectName . Sessions ;
29
- using AbpCompanyName . AbpProjectName . Web . Identity ;
30
29
using AbpCompanyName . AbpProjectName . Web . Startup ;
31
30
using AbpCompanyName . AbpProjectName . Web . Views . Shared . Components . TenantChange ;
32
- using Microsoft . AspNet . Identity ;
31
+ using Microsoft . AspNetCore . Identity ;
33
32
34
33
namespace AbpCompanyName . AbpProjectName . Web . Controllers
35
34
{
@@ -94,39 +93,26 @@ public ActionResult Login(string userNameOrEmailAddress = "", string returnUrl =
94
93
[ UnitOfWork ]
95
94
public virtual async Task < JsonResult > Login ( LoginViewModel loginModel , string returnUrl = "" , string returnUrlHash = "" )
96
95
{
97
- var loginResult = await GetLoginResultAsync ( loginModel . UsernameOrEmailAddress , loginModel . Password , GetTenancyNameOrNull ( ) ) ;
98
-
99
- await SignInAsync ( loginResult . User , loginResult . Identity , loginModel . RememberMe ) ;
100
- await UnitOfWorkManager . Current . SaveChangesAsync ( ) ;
101
-
102
- if ( string . IsNullOrWhiteSpace ( returnUrl ) )
103
- {
104
- returnUrl = GetAppHomeUrl ( ) ;
105
- }
106
-
96
+ returnUrl = NormalizeReturnUrl ( returnUrl ) ;
107
97
if ( ! string . IsNullOrWhiteSpace ( returnUrlHash ) )
108
98
{
109
99
returnUrl = returnUrl + returnUrlHash ;
110
100
}
111
101
102
+ var loginResult = await GetLoginResultAsync ( loginModel . UsernameOrEmailAddress , loginModel . Password , GetTenancyNameOrNull ( ) ) ;
103
+
104
+ await _signInManager . SignInAsync ( loginResult . Identity , loginModel . RememberMe ) ;
105
+ await UnitOfWorkManager . Current . SaveChangesAsync ( ) ;
106
+
112
107
return Json ( new AjaxResponse { TargetUrl = returnUrl } ) ;
113
108
}
114
109
115
110
public async Task < ActionResult > Logout ( )
116
111
{
117
- await HttpContext . Authentication . SignOutAsync ( AuthConfigurer . AuthenticationScheme ) ;
112
+ await _signInManager . SignOutAsync ( ) ;
118
113
return RedirectToAction ( "Login" ) ;
119
114
}
120
115
121
- private async Task SignInAsync ( User user , ClaimsIdentity identity = null , bool rememberMe = false )
122
- {
123
- if ( identity == null )
124
- {
125
- identity = await _userManager . CreateIdentityAsync ( user , AuthConfigurer . AuthenticationScheme ) ;
126
- }
127
-
128
- await _signInManager . SignOutAllAndSignInAsync ( identity , rememberMe ) ;
129
- }
130
116
131
117
private async Task < AbpLoginResult < Tenant , User > > GetLoginResultAsync ( string usernameOrEmailAddress , string password , string tenancyName )
132
118
{
@@ -173,10 +159,10 @@ public async Task<ActionResult> Register(RegisterViewModel model)
173
159
{
174
160
try
175
161
{
176
- ExternalLoginUserInfo externalLoginInfo = null ;
162
+ ExternalLoginInfo externalLoginInfo = null ;
177
163
if ( model . IsExternalLogin )
178
164
{
179
- externalLoginInfo = await _signInManager . GetExternalLoginUserInfo ( model . ExternalLoginAuthSchema ) ;
165
+ externalLoginInfo = await _signInManager . GetExternalLoginInfoAsync ( ) ;
180
166
if ( externalLoginInfo == null )
181
167
{
182
168
throw new ApplicationException ( "Can not external login!" ) ;
@@ -208,8 +194,8 @@ public async Task<ActionResult> Register(RegisterViewModel model)
208
194
if ( model . IsExternalLogin )
209
195
{
210
196
Debug . Assert ( externalLoginInfo != null ) ;
211
-
212
- if ( string . Equals ( externalLoginInfo . EmailAddress , model . EmailAddress , StringComparison . InvariantCultureIgnoreCase ) )
197
+
198
+ if ( string . Equals ( externalLoginInfo . Principal . FindFirstValue ( ClaimTypes . Email ) , model . EmailAddress , StringComparison . OrdinalIgnoreCase ) )
213
199
{
214
200
user . IsEmailConfirmed = true ;
215
201
}
@@ -218,8 +204,8 @@ public async Task<ActionResult> Register(RegisterViewModel model)
218
204
{
219
205
new UserLogin
220
206
{
221
- LoginProvider = externalLoginInfo . LoginInfo . LoginProvider ,
222
- ProviderKey = externalLoginInfo . LoginInfo . ProviderKey ,
207
+ LoginProvider = externalLoginInfo . LoginProvider ,
208
+ ProviderKey = externalLoginInfo . ProviderKey ,
223
209
TenantId = user . TenantId
224
210
}
225
211
} ;
@@ -237,7 +223,7 @@ public async Task<ActionResult> Register(RegisterViewModel model)
237
223
AbpLoginResult < Tenant , User > loginResult ;
238
224
if ( externalLoginInfo != null )
239
225
{
240
- loginResult = await _logInManager . LoginAsync ( externalLoginInfo . LoginInfo , tenant . TenancyName ) ;
226
+ loginResult = await _logInManager . LoginAsync ( externalLoginInfo , tenant . TenancyName ) ;
241
227
}
242
228
else
243
229
{
@@ -246,7 +232,7 @@ public async Task<ActionResult> Register(RegisterViewModel model)
246
232
247
233
if ( loginResult . Result == AbpLoginResultType . Success )
248
234
{
249
- await SignInAsync ( loginResult . User , loginResult . Identity ) ;
235
+ await _signInManager . SignInAsync ( loginResult . Identity , false ) ;
250
236
return Redirect ( GetAppHomeUrl ( ) ) ;
251
237
}
252
238
@@ -300,60 +286,60 @@ public ActionResult ExternalLogin(string provider, string returnUrl)
300
286
[ UnitOfWork ]
301
287
public virtual async Task < ActionResult > ExternalLoginCallback ( string returnUrl , string authSchema , string remoteError = null )
302
288
{
289
+ returnUrl = NormalizeReturnUrl ( returnUrl ) ;
290
+
303
291
if ( remoteError != null )
304
292
{
305
293
Logger . Error ( "Remote Error in ExternalLoginCallback: " + remoteError ) ;
306
294
throw new UserFriendlyException ( L ( "CouldNotCompleteLoginOperation" ) ) ;
307
295
}
308
296
309
- var tenancyName = GetTenancyNameOrNull ( ) ;
310
-
311
- var userInfo = await _signInManager . GetExternalLoginUserInfo ( authSchema ) ;
312
-
313
- if ( userInfo . LoginInfo == null || userInfo . LoginInfo . LoginProvider . IsNullOrEmpty ( ) || userInfo . LoginInfo . ProviderKey . IsNullOrEmpty ( ) )
297
+ var externalLoginInfo = await _signInManager . GetExternalLoginInfoAsync ( authSchema ) ;
298
+ if ( externalLoginInfo == null )
314
299
{
315
- Logger . Warn ( "Could not get LoginProvider and ProviderKey from external login." ) ;
316
- return RedirectToAction ( " Login" ) ;
300
+ Logger . Warn ( "Could not get information from external login." ) ;
301
+ return RedirectToAction ( nameof ( Login ) ) ;
317
302
}
318
303
319
- await HttpContext . Authentication . SignOutAsync ( AuthConfigurer . ExternalAuthenticationScheme ) ;
304
+ await _signInManager . SignOutAsync ( ) ;
305
+
306
+ var tenancyName = GetTenancyNameOrNull ( ) ;
320
307
321
- var loginResult = await _logInManager . LoginAsync ( userInfo . LoginInfo , tenancyName ) ;
308
+ var loginResult = await _logInManager . LoginAsync ( externalLoginInfo , tenancyName ) ;
322
309
323
310
switch ( loginResult . Result )
324
311
{
325
312
case AbpLoginResultType . Success :
326
- await SignInAsync ( loginResult . User , loginResult . Identity , true ) ;
327
-
328
- if ( string . IsNullOrWhiteSpace ( returnUrl ) )
329
- {
330
- returnUrl = GetAppHomeUrl ( ) ;
331
- }
332
-
313
+ await _signInManager . SignInAsync ( loginResult . Identity , false ) ;
333
314
return Redirect ( returnUrl ) ;
334
315
case AbpLoginResultType . UnknownExternalLogin :
335
- return await RegisterForExternalLogin ( userInfo ) ;
316
+ return await RegisterForExternalLogin ( externalLoginInfo ) ;
336
317
default :
337
318
throw _abpLoginResultTypeHelper . CreateExceptionForFailedLoginAttempt (
338
319
loginResult . Result ,
339
- userInfo . EmailAddress ?? userInfo . LoginInfo . ProviderKey ,
320
+ externalLoginInfo . Principal . FindFirstValue ( ClaimTypes . Email ) ?? externalLoginInfo . ProviderKey ,
340
321
tenancyName
341
322
) ;
342
323
}
343
324
}
344
325
345
- private async Task < ActionResult > RegisterForExternalLogin ( ExternalLoginUserInfo userInfo )
326
+ private async Task < ActionResult > RegisterForExternalLogin ( ExternalLoginInfo externalLoginInfo )
346
327
{
328
+ var email = externalLoginInfo . Principal . FindFirstValue ( ClaimTypes . Email ) ;
329
+ var nameinfo = ExternalLoginInfoHelper . GetNameAndSurnameFromClaims ( externalLoginInfo . Principal . Claims . ToList ( ) ) ;
330
+
347
331
var viewModel = new RegisterViewModel
348
332
{
349
- EmailAddress = userInfo . EmailAddress ,
350
- Name = userInfo . Name ,
351
- Surname = userInfo . Surname ,
333
+ EmailAddress = email ,
334
+ Name = nameinfo . name ,
335
+ Surname = nameinfo . surname ,
352
336
IsExternalLogin = true ,
353
- ExternalLoginAuthSchema = userInfo . LoginInfo . LoginProvider
337
+ ExternalLoginAuthSchema = externalLoginInfo . LoginProvider
354
338
} ;
355
339
356
- if ( userInfo . HasAllNonEmpty ( ) )
340
+ if ( nameinfo . name != null &&
341
+ nameinfo . surname != null &&
342
+ email != null )
357
343
{
358
344
return await Register ( viewModel ) ;
359
345
}
@@ -417,6 +403,26 @@ private string GetTenancyNameOrNull()
417
403
return _tenantCache . GetOrNull ( AbpSession . TenantId . Value ) ? . TenancyName ;
418
404
}
419
405
406
+ private string NormalizeReturnUrl ( string returnUrl , Func < string > defaultValueBuilder = null )
407
+ {
408
+ if ( defaultValueBuilder == null )
409
+ {
410
+ defaultValueBuilder = GetAppHomeUrl ;
411
+ }
412
+
413
+ if ( returnUrl . IsNullOrEmpty ( ) )
414
+ {
415
+ return defaultValueBuilder ( ) ;
416
+ }
417
+
418
+ if ( Url . IsLocalUrl ( returnUrl ) )
419
+ {
420
+ return returnUrl ;
421
+ }
422
+
423
+ return defaultValueBuilder ( ) ;
424
+ }
425
+
420
426
#endregion
421
427
422
428
#region Etc
@@ -448,6 +454,5 @@ await _notificationPublisher.PublishAsync(
448
454
}
449
455
450
456
#endregion
451
-
452
457
}
453
458
}
0 commit comments