Browse Source

Don't set password for external login.

pull/4979/head
Halil İbrahim Kalkan 6 years ago
parent
commit
6cac77d262
  1. 64
      modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs

64
modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs

@ -19,7 +19,6 @@ namespace Volo.Abp.Account.Web.Pages.Account
public class RegisterModel : AccountPageModel public class RegisterModel : AccountPageModel
{ {
protected IAccountAppService AccountAppService { get; } protected IAccountAppService AccountAppService { get; }
protected RandomPasswordGenerator RandomPasswordGenerator { get; }
[BindProperty(SupportsGet = true)] [BindProperty(SupportsGet = true)]
public string ReturnUrl { get; set; } public string ReturnUrl { get; set; }
@ -36,10 +35,9 @@ namespace Volo.Abp.Account.Web.Pages.Account
[BindProperty(SupportsGet = true)] [BindProperty(SupportsGet = true)]
public string ExternalLoginAuthSchema { get; set; } public string ExternalLoginAuthSchema { get; set; }
public RegisterModel(IAccountAppService accountAppService, RandomPasswordGenerator randomPasswordGenerator) public RegisterModel(IAccountAppService accountAppService)
{ {
AccountAppService = accountAppService; AccountAppService = accountAppService;
RandomPasswordGenerator = randomPasswordGenerator;
} }
public virtual async Task<IActionResult> OnGetAsync() public virtual async Task<IActionResult> OnGetAsync()
@ -80,11 +78,6 @@ namespace Volo.Abp.Account.Web.Pages.Account
{ {
await CheckSelfRegistrationAsync(); await CheckSelfRegistrationAsync();
var registerDto = new RegisterDto()
{
AppName = "MVC"
};
if (IsExternalLogin) if (IsExternalLogin)
{ {
var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync(); var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync();
@ -94,34 +87,40 @@ namespace Volo.Abp.Account.Web.Pages.Account
return RedirectToPage("./Login"); return RedirectToPage("./Login");
} }
registerDto.EmailAddress = Input.EmailAddress; await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress);
registerDto.UserName = Input.EmailAddress;
registerDto.Password = await RandomPasswordGenerator.CreateAsync();
} }
else else
{ {
ValidateModel(); await RegisterLocalUserAsync();
registerDto.EmailAddress = Input.EmailAddress;
registerDto.Password = Input.Password;
registerDto.UserName = Input.UserName;
} }
var userDto = await AccountAppService.RegisterAsync(registerDto); return Redirect(ReturnUrl ?? "~/"); //TODO: How to ensure safety? IdentityServer requires it however it should be checked somehow!
var user = await UserManager.GetByIdAsync(userDto.Id); }
await SignInManager.SignInAsync(user, isPersistent: false);
if (IsExternalLogin) protected virtual async Task RegisterLocalUserAsync()
{ {
await AddToUserLogins(user); ValidateModel();
}
return Redirect(ReturnUrl ?? "~/"); //TODO: How to ensure safety? IdentityServer requires it however it should be checked somehow! var userDto = await AccountAppService.RegisterAsync(
new RegisterDto
{
AppName = "MVC",
EmailAddress = Input.EmailAddress,
Password = Input.Password,
UserName = Input.UserName
}
);
var user = await UserManager.GetByIdAsync(userDto.Id);
await SignInManager.SignInAsync(user, isPersistent: true);
} }
protected virtual async Task AddToUserLogins(IdentityUser user) protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string emailAddress)
{ {
var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync(); var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id);
(await UserManager.CreateAsync(user)).CheckErrors();
(await UserManager.AddDefaultRolesAsync(user)).CheckErrors();
var userLoginAlreadyExists = user.Logins.Any(x => var userLoginAlreadyExists = user.Logins.Any(x =>
x.TenantId == user.TenantId && x.TenantId == user.TenantId &&
@ -130,13 +129,14 @@ namespace Volo.Abp.Account.Web.Pages.Account
if (!userLoginAlreadyExists) if (!userLoginAlreadyExists)
{ {
user.AddLogin(new UserLoginInfo( (await UserManager.AddLoginAsync(user, new UserLoginInfo(
externalLoginInfo.LoginProvider, externalLoginInfo.LoginProvider,
externalLoginInfo.ProviderKey, externalLoginInfo.ProviderKey,
externalLoginInfo.ProviderDisplayName externalLoginInfo.ProviderDisplayName
) ))).CheckErrors();
);
} }
await SignInManager.SignInAsync(user, isPersistent: true);
} }
protected virtual async Task CheckSelfRegistrationAsync() protected virtual async Task CheckSelfRegistrationAsync()

Loading…
Cancel
Save