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