【问题标题】:Implementing a Cookie Authorization Flow in IdentityServer4在 IdentityServer4 中实现 Cookie 授权流程
【发布时间】: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


    【解决方案1】:

    基本上,您的应用程序将包含三个部分。

    身份服务器 4

    API

    客户

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

    【讨论】:

    • 对,但是如何在客户端实现 cookie 流?我找不到任何文档
    • @Michael M 我不知道你有什么问题。但是这里是文档netchrisidentityserver4.readthedocs.io/en/dev/start/mvc.htmldocs.identityserver.io/en/aspnetcore2/quickstarts/… 如果您已正确设置 MVC 客户端,则可以将 [Authorize] 放入其中一项操作,如果您未获得授权,这会将您重定向到 Identity Server 登录。成功登录后,访问令牌保存在 cookie 中。然后,您可以在进行 API 调用时在标头中设置 cookie。查看链接。
    • 也许我在这里误解了一些东西,但我宁愿让 Web 管理登录页面,而不必将用户重定向到 API 站点。目标是在该服务器上没有任何视图,并且仅将其用于端点。
    • 这是您的第一个问题:API 和 Identity Server 应该是分开的。
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2017-07-30
    • 1970-01-01
    • 2017-10-10
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多