在IdentityServer中实现Cookie授权流4

问题描述 投票:0回答:1

我正在做一个基础设施项目,我想我需要一些指导;我翻遍了文档,似乎有点迷茫。我的项目是这样设置的:我有一个服务器(API),它通过Entity连接到MySQL数据库。

我有一个服务器(API),它通过EntityFramework连接到MySQL数据库。这个服务器有一个IdentityServer4的实例以及一些可消耗的端点,连接到这个服务器的将是一个桌面应用和几个移动应用。这些应用将连接到服务器,并通过ResourceOwnerPassword授权。(这部分似乎是可行的)。

我感到困惑的部分。我还有一个MVC Web服务器(Web),它应该能够通过AJAXCORS访问相同的端点。这个服务器是一个单独的组件,并且没有 IdentityServer4 包。我想有一个专门的网页(登录),它应该允许用户输入一个用户名密码记住我的复选框。这个表单应该通过HttpPost请求向客户端提交一个模型,然后将数据发送到API,API应该对数据库进行验证,成功后,发出一个授权cookie(基于RememberMe框的Sessionpersistent),Web可以消耗这个cookie来授权用户,并从中提取索赔。这个cookie也可以用来访问API中需要认证的端点。

目前,我可以使用Postman向API发送一个请求,它返回两个cookie:idsrv.session和.AspNetCore.Identity.Application。

我所设想的这个基础架构有什么问题吗?我应该如何使用Asp.NET Identity来识别和消费cookie(即,这样我就可以访问 User.Context (来自剃刀页)

以下是我从Web的Config文件中得到的。

        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
1个回答
0
投票

基本上,你的应用程序将有三个部分。

身份服务器 4

应用程序接口

客户

您将只有一个Identity Sever 4实例在运行,它可以授权多个API和多个客户端,如MVC、SPA等不同的流量。

© www.soinside.com 2019 - 2024. All rights reserved.