Allow to set username on social registration.

pull/17755/head
maliming 2 years ago
parent 3c21bbc641
commit f731de146d
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Volo.Abp.Account.Localization;
@ -54,4 +55,22 @@ public abstract class AccountPageModel : AbpPageModel
return exception.Message;
}
protected virtual async Task<string> GetUserNameFromEmail(string email)
{
var userName = email.Split('@')[0];
var existUser = await UserManager.FindByNameAsync(userName);
while (existUser != null)
{
var randomUserName = userName + RandomHelper.GetRandom(1000, 9999);
existUser = await UserManager.FindByNameAsync(randomUserName);
if (existUser == null)
{
userName = randomUserName;
break;
}
}
return userName;
}
}

@ -269,8 +269,9 @@ public class LoginModel : AccountPageModel
await IdentityOptions.SetAsync();
var emailAddress = info.Principal.FindFirstValue(AbpClaimTypes.Email);
var userName = await GetUserNameFromEmail(emailAddress);
var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id);
var user = new IdentityUser(GuidGenerator.Create(), userName, emailAddress, CurrentTenant.Id);
CheckIdentityErrors(await UserManager.CreateAsync(user));
CheckIdentityErrors(await UserManager.SetEmailAsync(user, emailAddress));

@ -12,7 +12,7 @@
<a href="@Url.Page("./Login", new {returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash})" class="text-decoration-none">@L["Login"]</a>
</strong>
<form method="post" class="mt-4">
@if (!Model.IsExternalLogin && Model.EnableLocalRegister)
@if (Model.EnableLocalRegister)
{
<abp-input asp-for="Input.UserName" auto-focus="true"/>
}

@ -75,7 +75,7 @@ public class RegisterModel : AccountPageModel
return Page();
}
private async Task TrySetEmailAsync()
protected virtual async Task TrySetEmailAsync()
{
if (IsExternalLogin)
{
@ -98,7 +98,8 @@ public class RegisterModel : AccountPageModel
return;
}
Input = new PostInput { EmailAddress = emailClaim.Value };
var userName = await GetUserNameFromEmail(emailClaim.Value);
Input = new PostInput { UserName = userName, EmailAddress = emailClaim.Value };
}
}
@ -122,7 +123,7 @@ public class RegisterModel : AccountPageModel
return RedirectToPage("./Login");
}
await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress);
await RegisterExternalUserAsync(externalLoginInfo, Input.UserName, Input.EmailAddress);
}
else
{
@ -156,11 +157,11 @@ public class RegisterModel : AccountPageModel
await SignInManager.SignInAsync(user, isPersistent: true);
}
protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string emailAddress)
protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string userName, string emailAddress)
{
await IdentityOptions.SetAsync();
var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id);
var user = new IdentityUser(GuidGenerator.Create(), userName, emailAddress, CurrentTenant.Id);
(await UserManager.CreateAsync(user)).CheckErrors();
(await UserManager.AddDefaultRolesAsync(user)).CheckErrors();
@ -202,7 +203,6 @@ public class RegisterModel : AccountPageModel
protected virtual async Task<List<ExternalProviderModel>> GetExternalProviders()
{
var schemes = await SchemeProvider.GetAllSchemesAsync();
return schemes

Loading…
Cancel
Save