最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use ASP.NET Core Identity without Username field - Stack Overflow

programmeradmin2浏览0评论

I have been working with .NET projects regularly, and I have always used the Identity package from .NET Core. Recently I had to work on a project where the main identifier is not a username, but the email. Because of this I wanted to find out how I can configure identity so that it does not use the username but I have not found anything that the package itself offers.

I would like to find a solution or configuration where I can remove the username field and have everything work correctly, or some kind of flag that tells the framework that the primary identifier is the email.

It seems strange to me that at this point the package does not support this natively, since the most common thing is that authentication is done with email and password.

I have been working with .NET projects regularly, and I have always used the Identity package from .NET Core. Recently I had to work on a project where the main identifier is not a username, but the email. Because of this I wanted to find out how I can configure identity so that it does not use the username but I have not found anything that the package itself offers.

I would like to find a solution or configuration where I can remove the username field and have everything work correctly, or some kind of flag that tells the framework that the primary identifier is the email.

It seems strange to me that at this point the package does not support this natively, since the most common thing is that authentication is done with email and password.

Share edited Mar 7 at 5:06 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 6 at 20:00 Isaac MoralesIsaac Morales 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

If you are using the default Identity UI package , it will automatically use the Email as the username by default.

There is no need to modify the Identity table to enable the username.

You could scaffold the register page to see how it works.

Codes:

        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = CreateUser();

                //here it set the Email as the username
                await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
                await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)

Database:

发布评论

评论列表(0)

  1. 暂无评论