【发布时间】:2020-10-19 17:22:38
【问题描述】:
我目前正在尝试为正在开发的移动应用程序创建一个 Web api。
我决定使用带有 Indentity Server 的 ASP.Net Core 3.1 来使用 OpenID Connect + PKCE 进行身份验证。
用户将通过 WebView 授权应用程序,访问令牌将用于对 api 的后续调用。 api的Controllers用[Authorize]属性修饰。
我尝试使用一些脚手架代码进行身份验证,但现在我在苦苦挣扎,因为 jwt 令牌在某些情况下似乎无法被识别。
在脚手架代码中,Startup 类中的ConfigureServices 方法称为services.AddDefaultIdentity<ApplicationUser>()。
问题在于,这还添加了我不需要/不想要的所有身份默认端点(/Account/Manage/Disable2fa、/Account/Manage/PersonalData 等)。
因此我将代码改为services.AddIdentity<ApplicationUser, IdentityRole>(),因为我现在多次阅读AddDefaultIdentity() 显然与AddIdentity() 相同但也调用.AddDefaultUI()
在测试登录流程后,发现 api 控制器在进行更改后不再接受 Authorization: Bearer ... Header 并且 api 调用总是被重定向到登录页面,就好像用户没有登录一样
当我将代码改回 AddDefaultIdentity 时,JWT 持有者令牌再次被正确验证,我可以使用 [Authorize] 属性访问控制器,但随后我又遇到了默认身份页面的问题...
我的问题是:如何使用 [Authorize] Arribute 和 JWT Bearer 标头保护我的 API 控制器,而不必包含默认身份 UI?
Startup.cs
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Munchify.Web.Data;
using Munchify.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using APIBackend.Web.Services;
namespace APIBackend.Web
{
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.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Identity/Account/Login";
});
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddTransient<IEmailSender, EmailSender>();
services.AddControllersWithViews();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/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.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
【问题讨论】:
标签: c# asp.net .net jwt asp.net-identity