繁体   English   中英

具有相同身份验证 Web API 的多个应用程序的授权工作流程是什么?

[英]What would be the authorization workflow for multiple applications with the same authentication web API?

我有两个应用程序,一个是使用 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? 谢谢!

为此,您可能需要考虑 IdentityServer 之类的东西。 它可以开箱即用地处理这些类型的场景——这是一个非常成熟的 .Net 解决方案,具有大量文档和示例代码。 https://identityserver.io/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM