关联您的 Microsoft 帐户。电子邮件条目。一次又一次

Associate your Microsoft account. Email entry. Again and Again

我只是通过使用 Azure Active Directory 进行身份验证的文档组装了一个 .Net Core 应用程序。

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/microsoft-logins?view=aspnetcore-5.0

我创建并链接了应用程序 ID 和密码,效果很好,但我正在尝试了解行为。

当我第一次登录时,它要求我将电子邮件与该帐户相关联。为什么是这样? 根据数据库,我的理解是您可以在内部用户成员数据库中没有真实用户存在的情况下登录。软认证,可以这么说。

当您将用户与电子邮件相关联时,它将创建一个成员记录,您可以将其与角色相关联。

  1. 为什么会出现分歧...之后我不是希望使用相同的用户名自动将它们创建为用户吗?

  2. 在我创建用户并第二次登录后,为什么应用程序“开箱即用”,要求他们第二次关联一个电子邮件地址?这将简单地作为重复条目出错。视图不应该在显示该文本之前先检查用户是否存在吗?

我知道我可以更改代码以使此行为更正常地工作,但我怀疑我在这里遗漏了一些关键的东西。

最重要的是,我可以尝试破解它并自动让他们登录并绕过这个电子邮件步骤,但是有没有办法通过设置来做到这一点?

谢谢!

将电子邮件与帐户相关联应该是预期的行为。

既然您选择了使用个人用户帐户登录您的.Net Core 应用程序,尽管您使用 Microsoft 帐户登录,但没有个人用户帐户当前应用程序。

您仍然需要被重定向回您可以设置电子邮件的网站,该电子邮件被识别为个人用户帐户。请参阅参考资料 here

  1. Why the bifurcation...after isn't it my desire to create them as a user automatically with the same username?

也许您的想法是合理的,但是会有用户使用与Microsoft 帐户不同的电子邮件。如果 AAD 工作帐户没有具有 Exchange Online 许可证的 O365 订阅,则并非所有 AAD 工作帐户都有邮箱。在这种情况下,他们必须使用另一个不同的电子邮件地址。

  1. After I've created the user and logged in a second time, why does the app "out of the box", ask them to associate an email address a second time? Which will simply error out as a duplicate entry. Shouldn't the view check if the user exists first before displaying that text?

这是因为您没有确认您的帐户。首次创建用户时(使用 Microsoft 帐户登录后单击 Register),您将被重定向到 /Identity/Account/RegisterConfirmation 页面,其中包含要模拟的 link电子邮件确认:

  1. Select Click here to confirm your account link.
  2. Select 登录 link 并使用相同的凭据登录。

那么您将不再需要注册

您正在关注的教程集 options.SignIn.RequireConfirmedAccount = true,请尝试如下设置 false 以跳过确认。

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
        .AddEntityFrameworkStores<ApplicationDbContext>();
    services.AddRazorPages();

    services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
    {
        microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ClientId"];
        microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
    });
}