first commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using IdentityServer4.Models;
|
||||
using JSMachine.WMS.Authorization.Model;
|
||||
|
||||
namespace JSMachine.WMS.Authorization
|
||||
{
|
||||
/// <summary>
|
||||
/// 从授权配置中解析 IdentityServer 的身份资源、API 作用域和客户端信息。
|
||||
/// </summary>
|
||||
public class AuthorizationConfigResolver
|
||||
{
|
||||
private static IConfiguration _configuration;
|
||||
/// <summary>
|
||||
/// 设置后续解析操作使用的配置对象。
|
||||
/// </summary>
|
||||
/// <param name="configuration">包含 AppSettings 配置节的配置对象。</param>
|
||||
public static void SetConfig(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 OpenID Connect 所需的标准身份资源。
|
||||
/// </summary>
|
||||
public static IEnumerable<IdentityResource> IdentityResources =>
|
||||
new List<IdentityResource>
|
||||
{
|
||||
new IdentityResources.OpenId(),
|
||||
new IdentityResources.Profile()
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 从 AppSettings:ApiScopes 读取 API 作用域配置。
|
||||
/// </summary>
|
||||
/// <returns>配置存在时返回 API 作用域集合,否则返回空值。</returns>
|
||||
public static IEnumerable<ApiScope>? GetApiScopes()
|
||||
{
|
||||
return _configuration
|
||||
.GetSection("AppSettings")
|
||||
.GetSection("ApiScopes")
|
||||
.Get<string[]>()
|
||||
?.Select(p => new ApiScope(p));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从 AppSettings:Clients 读取客户端配置,并转换为客户端凭据模式定义。
|
||||
/// </summary>
|
||||
/// <returns>配置存在时返回客户端集合,否则返回空值。</returns>
|
||||
public static IEnumerable<Client>? GetClients()
|
||||
{
|
||||
List<ClientInfo> clients = _configuration
|
||||
.GetSection("AppSettings")
|
||||
.GetSection("Clients")
|
||||
.Get<List<ClientInfo>>();
|
||||
|
||||
return clients?.Select(p => new Client
|
||||
{
|
||||
ClientId = p.ClientId,
|
||||
//用于身份验证的密钥
|
||||
ClientSecrets = new Secret[] { new Secret(p.Secret.Sha256()) },
|
||||
//客户端有权访问的范围
|
||||
AllowedScopes = p.AllowedScopes,
|
||||
//过期时间(秒)
|
||||
AccessTokenLifetime = p.AccessTokenLifetime,
|
||||
//没有交互式用户,使用 clientid/secret 进行身份验证
|
||||
AllowedGrantTypes = GrantTypes.ClientCredentials
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
namespace JSMachine.WMS.Authorization
|
||||
{
|
||||
/// <summary>
|
||||
/// 授权服务宿主启动入口,负责创建并运行 ASP.NET Core 主机。
|
||||
/// </summary>
|
||||
public static class AuthorizationEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 加载授权服务配置、构建 Web 主机并阻塞运行服务。
|
||||
/// </summary>
|
||||
public static void Start()
|
||||
{
|
||||
Host.CreateDefaultBuilder()
|
||||
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
|
||||
webBuilder.UseStartup<Startup>();
|
||||
})
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config
|
||||
.SetBasePath($"{hostingContext.HostingEnvironment.ContentRootPath}")
|
||||
.AddJsonFile("appSettingsAuthorization.json", false, true)
|
||||
.AddEnvironmentVariables();
|
||||
})
|
||||
.Build()
|
||||
.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="IdentityServer4" Version="4.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<NameOfLastUsedPublishProfile>F:\Gitfactory\yzzx_new\papermes1.0\JSMachine.WMS.Authorization\Properties\PublishProfiles\FolderProfile.pubxml</NameOfLastUsedPublishProfile>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace JSMachine.WMS.Authorization.Model
|
||||
{
|
||||
public class ClientInfo
|
||||
{
|
||||
public string ClientId { get; set; }
|
||||
public string Secret { get; set; }
|
||||
public List<string> AllowedScopes { get; set; }
|
||||
public int AccessTokenLifetime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using JSMachine.WMS.Authorization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
var config = builder.Configuration;
|
||||
AuthorizationConfigResolver.SetConfig(config);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services
|
||||
.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential() //���������û��֤�����ʹ�õĿ���������
|
||||
.AddInMemoryApiScopes(AuthorizationConfigResolver.GetApiScopes())
|
||||
.AddInMemoryClients(AuthorizationConfigResolver.GetClients())
|
||||
.AddInMemoryIdentityResources(AuthorizationConfigResolver.IdentityResources);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
app.UseIdentityServer();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:42074",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"JSMachine.PrintDCS.Authorization": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"applicationUrl": "http://localhost:5199",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "weatherforecast",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace JSMachine.WMS.Authorization
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers();
|
||||
|
||||
AuthorizationConfigResolver.SetConfig(Configuration);
|
||||
|
||||
services
|
||||
.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential() //���������û��֤�����ʹ�õĿ���������
|
||||
.AddInMemoryApiScopes(AuthorizationConfigResolver.GetApiScopes())
|
||||
.AddInMemoryClients(AuthorizationConfigResolver.GetClients())
|
||||
.AddInMemoryIdentityResources(AuthorizationConfigResolver.IdentityResources);
|
||||
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseIdentityServer();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"urls": "http://127.0.0.1:8077",
|
||||
|
||||
"AppSettings": {
|
||||
"ApiScopes": [ "PostOrderApi" ],
|
||||
"Clients": [
|
||||
{
|
||||
"ClientId": "用友测试用户",
|
||||
"Secret": "123456",
|
||||
"AllowedScopes": [ "PostOrderApi" ],
|
||||
"AccessTokenLifetime": 3600
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user