CORE Identity簡化模式服務(wù)端配置幫助類
Core Identity簡化(隱形)模式服務(wù)端在配置時(shí),可以通過新增Identity配置幫助類來更加方便地管理相關(guān)對象和資源。以下是針對服務(wù)端的配置幫助類的具體實(shí)現(xiàn)。 新增Identity配置幫助
Core Identity簡化(隱形)模式服務(wù)端在配置時(shí),可以通過新增Identity配置幫助類來更加方便地管理相關(guān)對象和資源。以下是針對服務(wù)端的配置幫助類的具體實(shí)現(xiàn)。
新增Identity配置幫助類
在服務(wù)端中,我們可以新增一個(gè)名為IdentityConfig的配置幫助類,用于管理可訪問的對象、客戶端配置以及用戶配置等信息。這樣可以更好地組織和管理相關(guān)資源,提高代碼的可讀性和維護(hù)性。
```csharp
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using IdentityServer4.Test;
namespace IdentityServerPeizhi
{
public class IdentityConfig
{
// 可訪問的對象
public static IEnumerable
{
return new List
{
new ApiResource("apiServer", "apiServer")
};
}
// 客戶端配置
public static IEnumerable
{
return new List
{
new Client
{
ClientId"clientId",
AllowedGrantTypes ,
ClientSecrets { new Secret("secret1122".Sha512()) },
AllowedScopes{ "apiServer" }
}
};
}
// 添加用戶配置
public static List
{
return new List
{
new TestUser
{
SubjectId "111",
Username "test111",
Password "123456"
}
};
}
// 身份資源配置
public static IEnumerable
{
return new List
{
new (),
new (),
new ()
};
}
}
}
```
Startup.cs配置服務(wù)
在Startup.cs文件中,通過ConfigureServices方法配置Identity Server服務(wù),包括添加IdentityServer、數(shù)據(jù)庫上下文、用戶存儲(chǔ)等內(nèi)容,以確保服務(wù)正常運(yùn)行。
```csharp
public void ConfigureServices(IServiceCollection services)
{
()
.AddDeveloperSigningCredential()
.AddInMemoryClients(())
.AddInMemoryApiResources(())
.AddInMemoryIdentityResources(())
.AddTestUsers(());
(("DefaultConnection")));
.AddEntityFrameworkStores
.AddDefaultTokenProviders();
();
}
```
配置Startup.cs中的Configure方法
在Startup.cs文件的Configure方法中,配置應(yīng)用程序的中間件,包括開發(fā)環(huán)境判斷、異常處理、靜態(tài)文件服務(wù)、Identity Server等內(nèi)容,并設(shè)置路由規(guī)則以確保應(yīng)用程序正常運(yùn)行。
```csharp
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (())
{
();
();
();
}
else
{
("/Home/Error");
}
();
();
(routes >
{
(
name: "default",
template: "{controllerHome}/{actionIndex}/{id?}");
});
}
```
登錄設(shè)置,過期時(shí)間等
在具體的登錄控制器中,可以配置登錄的驗(yàn)證邏輯,包括用戶名密碼驗(yàn)證、用戶信息獲取等操作,并設(shè)置過期時(shí)間等相關(guān)屬性,以確保用戶登錄的安全性和有效性。
```csharp
public async Task
{
if (_("test111", "123456"))
{
var user _("test111");
var props new AuthenticationProperties
{
IsPersistent true,
ExpiresUtc ((30))
};
await (HttpContext, , , props);
}
return View(model);
}
```
通過以上的配置和實(shí)現(xiàn),我們可以更好地管理Identity Server的相關(guān)資源和客戶端配置,確保服務(wù)端的安全性和穩(wěn)定性。同時(shí),在登錄設(shè)置中,可以控制登錄的有效期和安全性,提升用戶體驗(yàn)和系統(tǒng)安全性。