【发布时间】:2020-12-06 07:03:00
【问题描述】:
创建开箱即用的应用程序dotnet new angular --auth Individual。它工作正常。
现在我想稍微升级一下。假设我想显示用户位置的天气预报。通常(当我自己使用 WebApi 实现 OAuth 时)我会在应用程序请求 OAuth 令牌时从数据库中检索用户,然后注入声明说“UserID”=“1001”。当用户单击 Fetch Data 时 - 我会检索该用户 ID 和控制器中的位置并返回与该用户相关的数据..
如何使用 IdentityServer4 做到这一点 - 让用户进入控制器?
Startup.cs
public void ConfigureServices(IServiceCollection services)
...
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllersWithViews();
. . .
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, IConfiguration configuration)
...
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
【问题讨论】:
-
这里已经回答了这个问题:ASP.NET Core Identity - get current user
-
你救了我! :) 谢谢.. 我试过 UserManager.GetUserId 但它返回 null... 我没有在任何地方看到 User.FindFirst 或者我错过了..
标签: c# asp.net-core security identityserver4