【问题标题】:Is there a way to use Identity Server features in asp.net core 3.1 Rest-API有没有办法在 asp.net core 3.1 Rest-API 中使用 Identity Server 功能
【发布时间】:2020-02-22 00:15:01
【问题描述】:

我正在使用 asp.net core 3.1 在服务器中使用 Rest-API 进行项目,角度作为单页应用程序的前端。从每个客户端,用户需要提供他们的用户名和密码才能访问 Web API 的受保护部分。我想使用 Identity Server 的功能来访问 ASP.NET Core Identity UserManager、RoleManager 和 SignInManagers,以确定提供的用户名和密码是否有效。

我以前从未这样做过,我试图在互联网上搜索有关此的信息,但找不到太多信息。我想要一些有关使用哪些 nuget 包以及如何配置启动的帮助。

谢谢

【问题讨论】:

    标签: c# asp.net-core asp.net-web-api asp.net-core-identity asp.net-core-3.1


    【解决方案1】:

    我没有特别用 Angular 做过这个(我使用 vue),但它基本上是相同的概念。快速的 Google 搜索找到了一个 Angular 特定的教程,可能值得一看:https://fullstackmark.com/post/21/user-authentication-and-identity-with-angular-aspnet-core-and-identityserver。在 IS4 方面,我建议从 Asp.Net Identity IS4 模板开始 (https://identityserver4.readthedocs.io/en/latest/quickstarts/6_aspnet_identity.html)

    【讨论】:

      【解决方案2】:

      客户分为三种主要类型。 Official Document For Identity Clients Github link for the Official Identity Sample code

      为服务器到服务器的通信定义客户端

      在这种情况下,不存在交互式用户 - 服务(又名客户端)想要与 API(又名作用域)进行通信:

      public class Clients
      {
          public static IEnumerable<Client> Get()
          {
              return new List<Client>
              {
                  new Client
                  {
                      ClientId = "service.client",
                      ClientSecrets = { new Secret("secret".Sha256()) },
      
                      AllowedGrantTypes = GrantTypes.ClientCredentials,
                      AllowedScopes = { "api1", "api2.read_only" }
                  }
              };
          }
      }
      

      定义基于浏览器的 JavaScript 客户端(例如 SPA)用于用户身份验证和委托访问以及 API

      此客户端使用所谓的隐式流向 JavaScript 请求身份和访问令牌:

      var jsClient = new Client
      {
          ClientId = "js",
          ClientName = "JavaScript Client",
          ClientUri = "http://identityserver.io",
      
          AllowedGrantTypes = GrantTypes.Implicit,
          AllowAccessTokensViaBrowser = true,
      
          RedirectUris =           { "http://localhost:7017/index.html" },
          PostLogoutRedirectUris = { "http://localhost:7017/index.html" },
          AllowedCorsOrigins =     { "http://localhost:7017" },
      
          AllowedScopes =
          {
              IdentityServerConstants.StandardScopes.OpenId,
              IdentityServerConstants.StandardScopes.Profile,
              IdentityServerConstants.StandardScopes.Email,
      
              "api1", "api2.read_only"
          }
      };
      

      为用户身份验证和委托 API 访问定义服务器端 Web 应用程序(例如 MVC)

      交互式服务器端(或本机桌面/移动)应用程序使用混合流。此流程为您提供最佳安全性,因为访问令牌仅通过反向通道调用传输(并允许您访问刷新令牌):

      var mvcClient = new Client
      {
          ClientId = "mvc",
          ClientName = "MVC Client",
          ClientUri = "http://identityserver.io",
      
          AllowedGrantTypes = GrantTypes.Hybrid,
          AllowOfflineAccess = true,
          ClientSecrets = { new Secret("secret".Sha256()) },
      
          RedirectUris =           { "http://localhost:21402/signin-oidc" },
          PostLogoutRedirectUris = { "http://localhost:21402/" },
          FrontChannelLogoutUri =  "http://localhost:21402/signout-oidc",
      
          AllowedScopes =
          {
              IdentityServerConstants.StandardScopes.OpenId,
              IdentityServerConstants.StandardScopes.Profile,
              IdentityServerConstants.StandardScopes.Email,
      
              "api1", "api2.read_only"
          },
      };
      

      在 appsettings.json 中定义客户端

      AddInMemoryClients 扩展方法还支持从 ASP.NET Core 配置文件添加客户端。这允许您直接从 appsettings.json 文件定义静态客户端:

      "IdentityServer": {
        "IssuerUri": "urn:sso.company.com",
        "Clients": [
          {
            "Enabled": true,
            "ClientId": "local-dev",
            "ClientName": "Local Development",
            "ClientSecrets": [ { "Value": "<Insert Sha256 hash of the secret encoded as Base64 string>" } ],
            "AllowedGrantTypes": [ "implicit" ],
            "AllowedScopes": [ "openid", "profile" ],
            "RedirectUris": [ "https://localhost:5001/signin-oidc" ],
            "RequireConsent": false
          }
        ]
      }
      

      然后将配置部分传递给AddInMemoryClients方法:在Startup.cs

      AddInMemoryClients(configuration.GetSection("IdentityServer:Clients"))
      

      子类别或详细客户列表:

      1.客户凭证:

      2。资源所有者客户端

      3. JS OIDC 客户端

      4. JS OAuth 客户端

      5. MVC 混合客户端

      6. MVC 隐式客户端

      【讨论】:

        猜你喜欢
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 2021-04-19
        • 2020-09-25
        • 2020-09-19
        • 1970-01-01
        • 2018-10-17
        相关资源
        最近更新 更多