【发布时间】:2020-05-15 12:28:10
【问题描述】:
我有两个应用程序,一个是使用 Asp.Net Web Forms (app1) 用 VB.Net 编写的。第二个是在 C# 中使用 Asp.Net Core MVC (app2)。我想创建一个 Web API,对尝试访问任一应用程序的用户进行身份验证并在应用程序之间共享该授权令牌。如果用户从 app1 转到 app2,则 JWT 令牌将被视为有效,并且该用户将有效登录。如果同一用户在任何时候退出,它会删除 JWT 令牌并要求再次登录。
我已经构建了一个使用身份和实体框架运行的 Web api,如果您在身份中拥有一个帐户并成功执行自身授权,则该框架已经创建 JWT 令牌。我正在努力让 app2 接受 JWT 并以某种方式剖析它以查看用户在 app2 中的角色。这是我当前的 Startup.cs 页面,其中包含我如何连接 JwtBearer:
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)
{
var authManager = Configuration.GetSection("AuthenticationManager");
var key = TextEncodings.Base64Url.Decode(authManager.GetSection("AudienceSecret").Value);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.Authority = authManager.GetSection("Address").Value;
options.Audience = "my secret audience";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false
};
});
services.AddControllersWithViews();
services.AddMvc();
}
// 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();
}
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.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
是否有一种智能的方式来重定向登录并让 app2 的控制器操作让我的 [Authorize] 属性只从 API 中查看 JWT? 谢谢!
【问题讨论】:
-
是不同网址的网站吗?它们是网络应用还是 API?
-
您可以创建一个网关 API,作为单个入口点,然后从您的网关 API 对请求进行身份验证,并在经过身份验证后传递给相关 API
-
网站将位于不同的网址@joey。它们是调用身份验证 API 端点的 Web 应用程序
-
那么 2 个 web 应用程序正在调用一个 api,一个 web api?那是你的场景吗?
标签: c# entity-framework asp.net-core jwt asp.net-identity