一丑掺、客戶端模式介紹
客戶端模式(Client Credentials Grant)是指客戶端直接向認(rèn)證服務(wù)(Authorization Server)發(fā)送認(rèn)證請(qǐng)求墩弯,獲取token
,進(jìn)行認(rèn)證炊汤,一般適用于受信任的客戶端疆柔。
請(qǐng)求步驟為:
- 客戶端向認(rèn)證服務(wù)器進(jìn)行認(rèn)證劈伴,并請(qǐng)求一個(gè)訪問(wèn)令牌
token
密末;- 認(rèn)證服務(wù)器進(jìn)行認(rèn)證,通過(guò)之后跛璧,返回客戶端一個(gè)訪問(wèn)令牌严里。
二、創(chuàng)建認(rèn)證服務(wù)
- 創(chuàng)建一個(gè)認(rèn)證服務(wù)
IdentityServerCenter
,通過(guò)NuGet
安裝IdentityServer4
追城;- 添加配置資源與客戶端的文件刹碾,引入
using IdentityServer4.Models
public class Config
{
public static IEnumerable<ApiResource> GetResources()
{
return new List<ApiResource> {
new ApiResource {
Name = "ImageResource",
Scopes={ new Scope ("ImageResource")},//Scopes必須配置,否則獲取token時(shí)返回 invalid_scope
},
new ApiResource { Name = "FileResourse" },
new ApiResource { Name="Api", Scopes={new Scope ("Api") } }
};
}
public static IEnumerable<Client> GetClients()
{
return new List<Client> {
new Client {
ClientId = "ClientId",
AllowedGrantTypes =GrantTypes.ClientCredentials,//授權(quán)模式:客戶端模式
AllowedScopes={ "ImageResource","Api" }, //允許訪問(wèn)的資源 GetResources()中配置的
ClientSecrets={ new Secret { Value= "ClientSecret".Sha256(), Expiration=DateTime.Now.AddMinutes(5)} }
} };
}
}
- 注入
IdentityServer4
座柱,添加IdentityServer4
配置
public void ConfigureServices(IServiceCollection services)
{
//注入IdentityServer 添加IdentityServer4配置
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetResources()) //添加配置的資源ApiResource
.AddInMemoryClients(Config.GetClients());//添加配置的客戶端Client
// services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
- 使用
IdentityServer4
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//使用IdentityServer
app.UseIdentityServer();
}
- 啟動(dòng)項(xiàng)
由于未使用MVC
迷帜,訪問(wèn)該api
返回404物舒。IdentityServer4
提供一個(gè)地址可獲取相關(guān)配置項(xiàng)。
http://examplehostname.com.well-known/openid-configuration/
訪問(wèn) http://localhost:5000/.well-known/openid-configuration 將返回的信息序列化如下
scopes_supported": ["ImageResource", "Api", "offline_access"]
即為Config
中配置的訪問(wèn)的資源AllowedScopes
戏锹。
- 使用
postman
獲取token
grant_type
為客戶端授權(quán)client_credentials
,client_id
與Client_Secret
為Config
中配置的ClientId
與Secret
冠胯。接下來(lái)創(chuàng)建一個(gè)可訪問(wèn)的資源。
三锦针、創(chuàng)建資源服務(wù)
- 創(chuàng)建一個(gè)資源服務(wù)項(xiàng)目
ImageResourceApi
- 注入認(rèn)證
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(config => {
config.DefaultScheme = "Bearer";
}).AddIdentityServerAuthentication(option=> {
option.ApiName = "ImageResource";
option.Authority = "http://localhost:5000"; //認(rèn)證服務(wù)的url
option.ApiSecret = "ClientSecret".ToSha256();// 訪問(wèn)的secret
option.RequireHttpsMetadata = false;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
//使用認(rèn)證
app.UseAuthentication();
app.UseMvc();
}
- 啟動(dòng)資源服務(wù)荠察,返回401未授權(quán);
- 使用
postman
帶著token
請(qǐng)求資源服務(wù)ImageResourceApi
奈搜,請(qǐng)求成功悉盆。