我已经在 .net 核心 mvc 中实现了基于策略的授权 应用。法典:
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ValidUserAuthorization requirement)
{
List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("UserAlias", context.User.Identity.Name.Split('@')[0])
};
var isAuthorized = this.CubeServiceManager.GetDataFromService("CheckIfAuthorized", "GET", this.ConnectionStrings.BaseAddress, parameters);
if (isAuthorized == "true")
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
我在控制器级别添加了授权属性,每次都调用此异步方法。如果 isAuthorized 的值为 true,则用户已获得授权。 但是对于每次调用异步方法,都会进行服务调用以设置 isAuthorized 变量的值,我想为每个用户在每个会话中缓存该变量。 我怎样才能在这个授权处理程序中做到这一点。?
我建议使用memcached,Enyim客户端也被移植到.Net Core:https://github.com/cnblogs/EnyimMemcachedCore