要实现不使用ASP.NET Core Identity的IdentityServer4,可以按照以下步骤进行操作:
创建一个空的ASP.NET Core Web应用程序项目。
添加IdentityServer4 NuGet包。在项目文件中添加以下包引用:
using IdentityServer4.Models;
using System.Collections.Generic;
public class IdentityServerConfig
{
public static IEnumerable GetIdentityResources()
{
return new List
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
// 添加其他的身份资源
};
}
public static IEnumerable GetApiResources()
{
return new List
{
new ApiResource("api1", "My API")
// 添加其他的API资源
};
}
public static IEnumerable GetClients()
{
return new List
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api1" }
}
// 添加其他的客户端
};
}
}
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
.AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
.AddInMemoryClients(IdentityServerConfig.GetClients());
app.UseAuthentication();
app.UseIdentityServer();
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
[Authorize]
public ActionResult> Get()
{
return new string[] { "value1", "value2" };
}
}
这样就实现了不使用ASP.NET Core Identity的IdentityServer4。在这个示例中,使用了内存存储来配置IdentityServer4,但你也可以使用其他类型的存储,如数据库来存储身份资源、API资源和客户端配置信息。