c# - IdentityServer4 来自 .net 框架 4.6

标签 c# asp.net-mvc identity identityserver4

我的问题是关于 Identity Server 4 以及从预先存在的 .net 框架 MVC 应用程序调用它。

我已经完成了 ID4 的“快速启动”,直到它可以运行并正确响应示例 .net 核心 MVC 应用程序。

作为快速测试,我创建了一个基本的 .Net Framework MVC 应用程序并创建了一个启动 .cs 文件......

using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
using System.Collections.Generic;
using System.IdentityModel.Tokens;

[assembly: OwinStartup(typeof(MVC_OWIN_Client.Startup))]

namespace MVC_OWIN_Client
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            string baseClientAddress = "http://localhost:44301/";

            var authority = JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "Cookies"
            });

            app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                ClientId = "mvc.standard",
                Authority = "http://localhost:5000/",
                RedirectUri = baseClientAddress + "signin-oidc",
                PostLogoutRedirectUri = baseClientAddress + "signout-callback-oidc",
                ResponseType = "code id_token",
                Scope = "openid api1 offline_access",

                UseTokenLifetime = false,
                SignInAsAuthenticationType = "Cookies"
            });
        }
    }
}

在 ID4 配置中,我创建了一个客户端来匹配...
 public static IEnumerable<Client> GetClients()
 {

    ...

    new Client
        {
        ClientId = "mvc.standard",
            ClientName = "MVC Client2",
            AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

            ClientSecrets =
            {
                new Secret("secret".Sha256())
            },

            RedirectUris           = { "http://localhost:44301/signin-oidc" },
            PostLogoutRedirectUris = { "http://localhost:44301/signout-callback-oidc" },

            AllowedScopes =
            {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    "api1"
            },
            AllowOfflineAccess = true
    }
};

应用程序将通过登录和权限页面进行,ID4 最终表明用户已登录....
info: IdentityServer4.ResponseHandling.AuthorizeInteractionResponseGenerator[0]
      User consented to scopes: openid, api1, offline_access
info: IdentityServer4.Endpoints.AuthorizeCallbackEndpoint[0]
      Authorize endpoint response
      {
        "SubjectId": "2",
        "ClientId": "mvc.standard",
        "RedirectUri": "http://localhost:44301/signin-oidc",
        "State": "OpenIdConnect.AuthenticationProperties=pnxKmthLCWSNS1Tj8sBS1K4K-Erxq8_W3Sfj1gg3zXhTCqP-gKV-Hsfgh_pRLPYQcIdVJONhzA3VMdBNv4xqE7y8uX-pzEmeNKBYb0cPAh6Q9lm5knIS5ds9gccYKubK1U0NpnGAW7tw38brRzD7dEG-EkSgXqjnEGeS4pMCrFaG2CFwq08_-KyA85VufscpT3y9sL0hTLLYYbRiJhWIZBOM427piwaHpR-jbl7KXGo",
        "Scope": "openid api1 offline_access"
      }
info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[10]
      AuthenticationScheme: idsrv signed in.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 152.1613ms 200 text/html; charset=UTF-8

就是这样。 MVC 应用程序永远不会恢复,它会一直待在那里直到超时。

有没有人对 Identity Server 有任何经验并且能够告诉我是否有什么遗漏?提前感谢您的时间,安迪。

最佳答案

使用以下设置集成身份服务器 4 打开 Id 与 asp.net MVC .Net Framework cookie 基础身份验证连接。

确保您安装了以下 nuget

微软.Owin.Security

Microsoft.Owin.Security.Cookies

Microsoft.Owin.Security.OpenIdConnect

注意:使用您的应用程序本地主机端口更新端口号

public void Configuration(IAppBuilder app)
            {
   app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions {});

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            AuthenticationType = "oidc",
            SignInAsAuthenticationType = "Cookies",
            Authority = "http://localhost:5000/",
            RedirectUri = "http://localhost:44301/signin-oidc",
            PostLogoutRedirectUri = "http://localhost:44301/signout-callback-oidc",
            ClientId = "CP",
            ResponseType = "id_token",
            Scope = "openid profile",
            UseTokenLifetime = false,
            RequireHttpsMetadata = false,
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                SecurityTokenValidated = (context) =>
                {
                    var identity = context.AuthenticationTicket.Identity;
                    var name = identity.Claims.FirstOrDefault(c => c.Type == identity.NameClaimType)?.Value;

                    return Task.FromResult(0);
                }
            }
        });
    }

关于c# - IdentityServer4 来自 .net 框架 4.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48769069/

相关文章:

描述无效的 C# 枚举

authentication - Saml2AuthenticationModule 类去了哪里?

asp.net - 如何使用 ASP.NET 身份模型进行 WCF 服务授权和身份验证

sql - 有没有办法让身份规范显示在表设计器中“允许空值”列旁边?

c# - 在 C# 中获取数字部分值的最快方法是什么?

c# - 如何在不获取整个文档的情况下获取字段的值?

c# - 具有多个可选参数调用不同操作的 Asp.net 核心路由

c# - 在 LINQ 语句中使用部分类属性

jquery - 多个文件的 Ajax 上传脚本,jQuery ASP.NET MVC

c# - 在javascript中转义内联c#脚本中的双引号