c# - Azureb2c 登录未连接到帐户控制器
问题描述
我有一个使用 Azure B2C 的剃须刀应用程序,我已经开发了一段时间。登录按钮不起作用,因此我使用默认向导创建了一个新应用程序,如果我更改 azure 应用程序中的本地主机,该应用程序确实可以工作。我试图找出这两个应用程序之间的区别。
它们都具有相同的 _LoginPartial.cshtml 文件,并且在 Layout 文件中为登录按钮设置了相同的设置,但不起作用的那个没有用于调用帐户的 href 数据。当我查看 f12 调试器时,登录链接呈现如下:<a class="nav-link text-dark" href>Sign in</a>
工作的应用程序呈现如下:<a class="nav-link text-dark" href="/AzureADB2C/Account/SignIn">Sign in</a>
这显然要好得多。
两者都通过 nuget 包运行 Microsoft.AspNetCore.Authentication.AzureADB2C.UI。
损坏的应用程序有很多其他的东西,显然新的没有,telerik,azure storage,sql,实体框架等,但在登录的配置等周围一切看起来都一样。我在下面的损坏版本中包含了启动等,以防万一。请问有人能发现这个问题吗?
登录部分:
@using Microsoft.AspNetCore.Authentication.AzureADB2C.UI
@using Microsoft.Extensions.Options
@inject IOptionsMonitor<AzureADB2COptions> AzureADB2COptions
@{
var options = AzureADB2COptions.Get(AzureADB2CDefaults.AuthenticationScheme);
}
<ul class="navbar-nav">
@if (User.Identity.IsAuthenticated)
{
@if (!string.IsNullOrEmpty(options.EditProfilePolicyId))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="AzureADB2C" asp-controller="Account" asp-action="EditProfile">
<span class="text-dark">Hello @User.Identity.Name!</span>
</a>
</li>
}
else
{
<li class="nav-item">
<span class="navbar-text text-dark">Hello @User.Identity.Name!</span>
</li>
}
<li class="nav-item">
<a class="nav-link text-dark" asp-area="AzureADB2C" asp-controller="Account" asp-action="SignOut">Sign out</a>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="AzureADB2C" asp-controller="Account" asp-action="SignIn">Sign in</a>
</li>
}
</ul>
启动设置
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50209",
"sslPort": 44370
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"FliveRetry": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
应用设置
{
"AzureAdB2C": {
"Instance": "https://xxxx.b2clogin.com/tfp/",
"ClientId": "xxxx-xx-xx-xx-xx",
"CallbackPath": "/signin-oidc",
"Domain": "xxxx.onmicrosoft.com",
"SignUpSignInPolicyId": "B2C_1_signupsignin",
"ResetPasswordPolicyId": "B2C_1_resetpassword",
"EditProfilePolicyId": "B2C_1_editprofile"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"FliveRetryContext": "Server=(localdb)\\mssqllocaldb;Database=xxxx-566cf99c-25d6-42a3-9260-5626bc8829b2;Trusted_Connection=True;MultipleActiveResultSets=true",
}
}
启动.cs
using System;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureADB2C.UI;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Serialization;
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using FliveRetry.Models;
using FliveRetry.Models.PTs;
using FliveRetry.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.HttpsPolicy;
namespace FliveRetry
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.Cookie.Name = ".Flive";
});
services.AddMemoryCache();
services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
.AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));
services.AddRazorPages().AddNewtonsoftJson
(options => {
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
services.AddDbContext<FliveRetryContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("xxxxx")));
services.AddRazorPages();
services.AddMvc()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new DefaultContractResolver());
services.AddMvc();
services.AddMvc().AddRazorRuntimeCompilation();
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
services.AddScoped<IPtNoteRepository, PtNoteRepository>();
//services.AddSingleton<PtNoteRepository>();
// Add Kendo UI services to the services container
services.AddKendo();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var supportedCultures = new[] { new CultureInfo("en-AU") };
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-AU"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
//app.UseHttpContextItemsMiddleware();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
app.UseStaticFiles();
app.UseSession();
}
}
}
解决方案
似乎Microsoft.AspNetCore.Authentication.AzureADB2C.UI Nuget 包已被标记为过时,微软建议开始使用Microsoft.Identity.Web包。
我让内置控制器(例如 /AzureADB2C/Account/SignIn)在 .NET Core 2.2 Web 应用程序上运行,但是一旦我升级到 3.1,它们就停止工作(404 错误)。
解决方案是删除对 Microsoft.AspNetCore.Authentication.AzureADB2C.UI 的所有引用并替换为Microsoft.Identity.Web.UI包。
然后,您必须在 Razor 页面中更改以下实例:
asp-area="AzureADB2C"
和:
asp-area="MicrosoftIdentity"
如果您想要参考设置,请查看此示例: https ://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/1-WebApp-OIDC/1-5 -B2C
推荐阅读
- javascript - 我不知道如何使用更新的 json 数据呈现新页面
- java - 用户输入中不太可能找到 Java 中的 Character.LINE_SEPARATOR 吗?
- php - 获取字符串中第一个带字母的点的索引
- c# - 使用 ADO,使用 SqlDataAdapter.FillSchema 清除 @ReturnValues 和其他标记为输出的存储过程参数
- javascript - 仅在数据存在时渲染 FlatList
- ios - 在 ARM macOS 中运行 iOS 应用程序的有效架构问题
- javascript - 未捕获的类型错误:将对象添加到 Sharepoint 中的字段时,无法读取未定义的属性“toLowerCase”
- java - Spark 错误:所有数据节点都坏了。中止
- python - 从交互式网站地图中抓取数据
- r - 当此值等于 1 时,将下 n 个值设置为零