在 Core 6 中使用个人帐户和 Microsoft.Identity.Web 进行身份验证

问题描述 投票:0回答:0

随着 AzureAD.UI 的弃用,我想构建一个使用个人帐户和 Microsoft.Identity.Web 进行身份验证的 Web 应用程序。我已经尝试了多种方法并且我认为我有两次,只是在完成 Azuree AD 登录后得到一个“加载外部登录信息时出错”。

我开始使用 Visual Studio 2022 中的个人帐户身份验证类型的新项目。

添加了 Microsoft.Identity.Web nuget 包

这是我的 Program.cs

using AuthTest.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration);
builder.Services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
    // Set the name of the claim in the JWT token that represents the user's display name
    options.TokenValidationParameters.NameClaimType = "name";
});



builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();

app.Run();

这是我的 appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-AuthTest;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "**********",
    "TenantId": "***********",
    "ClientId": "****************",
    "CallbackPath": "/signin-oidc",
    "ClientSecret": "******************",
    "ClientCertificates": []
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

有人看到或知道我做错了什么吗?

asp.net-identity openid-connect identity dotnetopenauth microsoft.identity.web
© www.soinside.com 2019 - 2024. All rights reserved.