diff --git a/IRaCIS.Core.API/Controllers/ExtraController.cs b/IRaCIS.Core.API/Controllers/ExtraController.cs index ac6592bd8..d072ef029 100644 --- a/IRaCIS.Core.API/Controllers/ExtraController.cs +++ b/IRaCIS.Core.API/Controllers/ExtraController.cs @@ -1,4 +1,5 @@ -using Amazon.Auth.AccessControlPolicy; +using AlibabaCloud.SDK.Sts20150401; +using Amazon.Auth.AccessControlPolicy; using Amazon.SecurityToken; using AutoMapper; using Azure.Core; @@ -21,6 +22,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Org.BouncyCastle.Tls; using RestSharp; using RestSharp.Authenticators; using System; @@ -182,6 +184,69 @@ namespace IRaCIS.Api.Controllers } + #region 老项目依赖 + + [HttpGet("user/GenerateSTS")] + public IResponseOutput GenerateSTS([FromServices] IOptionsMonitor options) + { + + var ossOptions = options.CurrentValue.AliyunOSS; + + var client = new Client(new AlibabaCloud.OpenApiClient.Models.Config() + { + AccessKeyId = ossOptions.AccessKeyId, + AccessKeySecret = ossOptions.AccessKeySecret, + Endpoint = "sts.cn-hangzhou.aliyuncs.com" + }); + + var assumeRoleRequest = new AlibabaCloud.SDK.Sts20150401.Models.AssumeRoleRequest(); + // 将设置为自定义的会话名称,例如oss-role-session。 + assumeRoleRequest.RoleSessionName = $"session-name-{NewId.NextGuid()}"; + // 将替换为拥有上传文件到指定OSS Bucket权限的RAM角色的ARN。 + assumeRoleRequest.RoleArn = ossOptions.RoleArn; + //assumeRoleRequest.RoleArn = "acs:ram::1899121822495495:role/webdirect"; + assumeRoleRequest.DurationSeconds = ossOptions.DurationSeconds; + var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); + var response = client.AssumeRoleWithOptions(assumeRoleRequest, runtime); + var credentials = response.Body.Credentials; + + var tempToken = new AliyunOSSTempToken() + { + AccessKeyId = credentials.AccessKeyId, + AccessKeySecret = credentials.AccessKeySecret, + + //转为服务器时区,最后统一转为客户端时区 + Expiration = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse(credentials.Expiration), TimeZoneInfo.Local), + SecurityToken = credentials.SecurityToken, + + + Region = ossOptions.Region, + BucketName = ossOptions.BucketName, + EndPoint = ossOptions.EndPoint, + ViewEndpoint = ossOptions.ViewEndpoint, + PreviewEndpoint = ossOptions.PreviewEndpoint + + }; + + // 返回STS令牌信息给前端 + var stsToken = new + { + AccessKeyId = credentials.AccessKeyId, + AccessKeySecret = credentials.AccessKeySecret, + SecurityToken = credentials.SecurityToken, + Expiration = credentials.Expiration, + + Region = ossOptions.Region, + BucketName = ossOptions.BucketName, + ViewEndpoint = ossOptions.ViewEndpoint, + + }; + + return ResponseOutput.Ok(stsToken); + + } + + #endregion [HttpGet("User/UserRedirect")]