用户名已被使用,但实际上还没有被使用 ASP.NET Core

huangapple go评论97阅读模式
英文:

Username is already taken when actually it is not ASP.NET Core

问题

背景

这个问题是我之前问题的延续。在那个问题中,我遇到了一个问题,无法登录到现有用户。多亏了Md Farid Uddin Kiron的帮助,我通过更改appsettings.json中的连接字符串解决了这个问题。但现在我遇到了另一个问题。

问题/困扰

我遇到了一个不寻常的问题,当我尝试创建一个新帐户时,出现一个异常,说用户名已经存在。我只有一个具有唯一用户名(电子邮件地址)的用户。我无法理解为什么会发生这种情况。

我将添加处理注册过程的函数,但我不认为问题是由它引起的。

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
    returnUrl ??= Url.Content("~/");

    ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

    if (ModelState.IsValid)
    {
        var user = new ApplicationUser
                {
                    UserName = Input.Email,
                    Email = Input.Email,
                    FirstName = Input.FirstName,
                    LastName = Input.LastName
                };

        var result = await _userManager.CreateAsync(user, Input.Password);

        if (result.Succeeded)
        {
            _logger.LogInformation("User created a new account with password.");

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
            code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
            var callbackUrl = Url.Page("/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                    protocol: Request.Scheme);

            await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                              $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

            if (_userManager.Options.SignIn.RequireConfirmedAccount)
            {
                return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
            }
            else
            {
                await _signInManager.SignInAsync(user, isPersistent: false);
                return LocalRedirect(returnUrl);
            }
        }

        foreach (var error in result.Errors)
        {
            ModelState.AddModelError(string.Empty, error.Description);
        }
    }

    // 如果我们走到这一步,说明出现了问题,重新显示表单
    return Page();
}

CreateAsync

//
// 摘要:
//     创建给定用户的异步操作中的后备存储中指定用户,使用给定密码。
//
// 参数:
//   user:
//     要创建的用户。
//
//   password:
//     用户的密码,将其散列和存储。
//
// 返回:
//     表示异步操作的 System.Threading.Tasks.Task,包含操作的 Microsoft.AspNetCore.Identity.IdentityResult。
public virtual Task<IdentityResult> CreateAsync(TUser user);

[AsyncStateMachine(typeof(UserManager<>.<CreateAsync>d__78))]
public virtual Task<IdentityResult> CreateAsync(TUser user, string password);

如果您需要更多的代码,请随时要求。感谢您的时间。

英文:

Background

This question is a continuation of my previous question. In that question, I've faced a problem where I could not log in to an existing user. I solved it thanks to Md Farid Uddin Kiron. I have changed my connection string in the appsettings.json. It solved my problem but now I have another problem.

Question/Problem

I am facing an unusual problem, when I try to create a new account I get an exception that says that the username already exists. I have only one user with a unique username (email address). I can't understand why it is happening.

I am adding the function that handles the signup process, but I don't believe the problem comes from it.

public async Task&lt;IActionResult&gt; OnPostAsync(string returnUrl = null)
{
    returnUrl ??= Url.Content(&quot;~/&quot;);

    ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

    if (ModelState.IsValid)
    {
        var user = new ApplicationUser
                {
                    UserName = Input.Email,
                    Email = Input.Email,
                    FirstName = Input.FirstName,
                    LastName = Input.LastName
                };

        var result = await _userManager.CreateAsync(user, Input.Password);

        if (result.Succeeded)
        {
            _logger.LogInformation(&quot;User created a new account with password.&quot;);

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
            code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
            var callbackUrl = Url.Page(&quot;/Account/ConfirmEmail&quot;,
                    pageHandler: null,
                    values: new { area = &quot;Identity&quot;, userId = user.Id, code = code, returnUrl = returnUrl },
                    protocol: Request.Scheme);

            await _emailSender.SendEmailAsync(Input.Email, &quot;Confirm your email&quot;,
                                              $&quot;Please confirm your account by &lt;a href=&#39;{HtmlEncoder.Default.Encode(callbackUrl)}&#39;&gt;clicking here&lt;/a&gt;.&quot;);

            if (_userManager.Options.SignIn.RequireConfirmedAccount)
            {
                return RedirectToPage(&quot;RegisterConfirmation&quot;, new { email = Input.Email, returnUrl = returnUrl });
            }
            else
            {
                await _signInManager.SignInAsync(user, isPersistent: false);
                return LocalRedirect(returnUrl);
            }
        }

        foreach (var error in result.Errors)
        {
            ModelState.AddModelError(string.Empty, error.Description);
        }
    }

    // If we got this far, something failed, redisplay form
    return Page();
}

CreateAsync

//
// Summary:
//     Creates the specified user in the backing store with given password, as an asynchronous operation.
//
// Parameters:
//   user:
//     The user to create.
//
//   password:
//     The password for the user to hash and store.
//
// Returns:
//     The System.Threading.Tasks.Task that represents the asynchronous operation, containing
//     the Microsoft.AspNetCore.Identity.IdentityResult of the operation.
public virtual Task&lt;IdentityResult&gt; CreateAsync(TUser user);

[AsyncStateMachine(typeof(UserManager&lt;&gt;.&lt;CreateAsync&gt;d__78))]
public virtual Task&lt;IdentityResult&gt; CreateAsync(TUser user, string password);

If you need more code do not be afraid to ask for it.

Thanks for your time.

答案1

得分: 0

Alright, with the help of Cetin Basoz we have solved the issue, here is a step-by-step guide to how we solved it.

Step 1

We created a zoom meeting where we went through the code and debuted it step by step. One thing that popped up while we were in zoom was that some columns in the SQL table could not be null. Then I realized that the version of the solution did not match up with the SQL table of users, meaning, that on the laptop I used an older code that did not have some of the requirements to run with the SQL table.

  • Reminder No. 1: Always ensure your solution can handle the SQL table.

Step 2 - only for those who have email confirmation

After we fixed the SQL errors I found out that my laptop was missing the API key that is used to send email confirmations. My API key was saved as an environment variable on my main PC. This is a problem because if I would want to upload my code to the web, it would not be able to send emails because the API key will be missing. <br>
To solve this, I wanted to put the key inside my code, but by doing this I realized that people would have access to it. To save it somewhere "more" safe I saved it in my Resources.resx file and access it like this:

public string APIKey { get; }

public EmailSender()
{
    this.APIKey = Properties.Resources.SendGridAPIKey; // Access right here.
}
  • Reminder No. 2: Be aware of where you save your variables. Make sure that they will be accessible from anywhere.

英文:

Alright, with the help of Cetin Basoz we have solved the issue, here is a step-by-step guide to how we solved it.

Step 1

We created a zoom meeting where we went through the code and debuted it step by step. One thing that popped up while we were in zoom was that some columns in the SQL table could not be null. Then I realized that the version of the solution did not match up with the SQL table of users, meaning, that on the laptop I used an older code that did not have some of the requirements to run with the SQL table.

  • Reminder No. 1: Always ensure your solution can handle the SQL table.

Step 2 - only for those who have email confirmation

After we fixed the SQL errors I found out that my laptop was missing the API key that is used to send email confirmations. My API key was saved as an environment variable on my main PC. This is a problem because if I would want to upload my code to the web, it would not be able to send emails because the API key will be missing. <br>
To solve this, I wanted to put the key inside my code, but by doing this I realized that people would have access to it. To save it somewhere "more" safe I saved it in my Resources.resx file and access it like this:

public string APIKey { get; }

public EmailSender()
{
    this.APIKey = Properties.Resources.SendGridAPIKey; // Access right here.
}
  • Reminder No. 2: Be aware of where you save your variables. Make sure that they will be accessible from anywhere.

huangapple
  • 本文由 发表于 2023年3月15日 20:43:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定