鉴权
This commit is contained in:
@@ -197,5 +197,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
var treeData = Common.Helper.TreeHelper.SetTree(vueRouterModels);
|
||||
return Result.Success().SetData(treeData);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
public Result AuthorizeTest()
|
||||
{
|
||||
return Result.Success();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
global using System;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Yi.Framework.WebCore.BuilderExtend;
|
||||
using Yi.Framework.Core;
|
||||
@@ -11,7 +12,6 @@ using Yi.Framework.WebCore.AttributeExtend;
|
||||
using Yi.Framework.WebCore.SignalRHub;
|
||||
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Configuration.AddCommandLine(args);
|
||||
builder.WebHost.UseUrls(builder.Configuration.GetValue<string>("StartUrl"));
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace Yi.Framework.WebCore
|
||||
long resId = 0;
|
||||
try
|
||||
{
|
||||
claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
|
||||
claimlist = httpContext.User.Claims;
|
||||
resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value);
|
||||
}
|
||||
catch
|
||||
|
||||
@@ -5,9 +5,11 @@ using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.Helper;
|
||||
using Yi.Framework.Common.IOCOptions;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Core;
|
||||
|
||||
namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
@@ -25,15 +27,32 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnAuthenticationFailed = (context) =>
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
OnMessageReceived = (context) =>
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
OnChallenge = (context) =>
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
};
|
||||
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,//是否验证Issuer
|
||||
ValidateAudience = true,//是否验证Audience
|
||||
ValidateLifetime = true,//是否验证失效时间
|
||||
ValidateIssuerSigningKey = true,//是否验证SecurityKey
|
||||
ValidAudience = jwtOptions.Audience,//Audience
|
||||
ValidIssuer = jwtOptions.Issuer,//Issuer,这两项和前面签发jwt的设置一致
|
||||
IssuerSigningKey = new RsaSecurityKey(RSAFileHelper.GetPublicKey())
|
||||
ClockSkew = TimeSpan.Zero,//过期缓冲时间
|
||||
ValidateIssuer = true,//是否验证Issuer
|
||||
ValidateAudience = true,//是否验证Audience
|
||||
ValidateLifetime = true,//是否验证失效时间
|
||||
ValidateIssuerSigningKey = true,//是否验证SecurityKey
|
||||
ValidAudience = jwtOptions.Audience,//Audience
|
||||
ValidIssuer = jwtOptions.Issuer,//Issuer,这两项和前面签发jwt的设置一致
|
||||
IssuerSigningKey = new RsaSecurityKey(RSAFileHelper.GetPublicKey())
|
||||
};
|
||||
});
|
||||
return services;
|
||||
|
||||
Reference in New Issue
Block a user