簡體   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