[.Net Core][IdentityServer4] .Net core mvc 集成identityserver4 的方法,避坑

在配置IdentityServer4 的时候 我们可以添加多种认证方式如:oidc , bearer。 

区别:oidc 方式需要页面跳转到IdentityServer 的登录页面进行登录,然后跳转回去。

           bearer 则只需要携带token进行请求即可。

oidc 方式适用grant type如下:

Implicit:具体参考https://docs.identityserver.io/en/release/quickstarts/7_javascript_client.html#refjavascriptquickstart

var config = {
    authority: "http://localhost:5000",
    client_id: "js",
    redirect_uri: "http://localhost:5003/callback.html",
    response_type: "id_token token",
    scope:"openid profile api1",
    post_logout_redirect_uri : "http://localhost:5003/index.html",
};
var mgr = new Oidc.UserManager(config);

bearer 方式下,请求token的地址为:

https://identityserver-host/connect/token,

content-type:application/x-www-form-urlencoded

post body 则根据不同grant_type 需要包含的参数不同。

具体如下:

参考:https://docs.identityserver.io/en/release/topics/grant_types.html

ClientCredentials

      grant_type: idsConfig.responseType,
      client_id: idsConfig.clientId,
      client_secret: idsConfig.clientSecret,
      scope: idsConfig.clientScope

ResourceOwnerPassword

      username,
      password,
      grant_type: idsConfig.responseType,
      client_id: idsConfig.clientId,
      client_secret: idsConfig.clientSecret,
      scope: idsConfig.clientScope

需要注意的是:identityserver host必须为https配置,否则会导致请求异常。

原文地址:https://www.cnblogs.com/blackcatpolice/p/13905021.html