【发布时间】:2020-08-17 10:13:24
【问题描述】:
我正在从事一个基础设施项目,我想我可以使用一些指导;我已经把文档倒过来了,似乎有点迷失了。我的项目设置如下。
我有一个通过 EntityFramework 连接到 MySQL 数据库的服务器 (API)。这个服务器有一个 IdentityServer4 的实例以及一些可消费的端点。 连接到此服务器的将是一个桌面应用程序和几个移动应用程序。这些应用程序将连接到服务器并通过 ResourceOwnerPassword 授权进行授权。 (这部分似乎有效。)
我感到困惑的部分:我还有一个 MVC Web 服务器 (Web),它应该能够通过 AJAX/CORS 访问相同的端点。此服务器是一个单独的程序集,没有 IdentityServer4 包。我想要一个专用网页(/登录),允许用户输入用户名/密码/记住我复选框。然后表单应该通过 HttpPost 请求向客户端提交模型,然后将数据发送到 API,API 应该对数据库进行身份验证,并在成功时发出授权 cookie(基于 RememberMe 框的会话/持久),Web 可以然后消费以授权用户并从中提取索赔。该 cookie 还将用于访问 API 中需要身份验证的端点。
目前,我可以使用 Postman 向 API 发送请求,该请求返回两个 cookie:idsrv.session 和 .AspNetCore.Identity.Application。
这个基础设施是否有我预想的问题?我应该如何使用 Asp.NET Identity 识别和使用 cookie(即我可以从 Razor 页面访问 User.Context)
以下内容来自我的 Web 配置文件:
public void ConfigureServices(IServiceCollection services) {
services.Configure<CookiePolicyOptions>(options => {
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(o => {
o.DefaultScheme = "Cookies";
o.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", o => {
o.Authority = Configuration["Authority"];
o.RequireHttpsMetadata = false;
o.ClientId = Configuration["ClientId"];
o.ClientSecret = Configuration["ClientSecret"];
o.ResponseType = "code";
o.SaveTokens = true;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if(env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
else {
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-core asp.net-identity identityserver4