我正在尝试在asp.net核心中设置自定义策略提供程序。我需要将几个自定义授权属性传递给提供者,但在如何做到这一点上却很挣扎。
我已将其设置为当前接受一个枚举数组,并且工作正常。但是,我想再添加2个枚举数组作为授权属性的其他可选参数。
现在的属性:
[LEMClaimAuthorize(new ELocation[] { ELocation.Indy, ELocation.Columbus })]
希望它像这样工作:
[LEMClaimAuthorize(new ELocation[] { ELocation.Indy, ELocation.Columbus },
new EEntity[] { EEntity.JobTool })]
LEMClaimAuthorizeAttribute是:
public class LEMClaimAuthorizeAttribute : AuthorizeAttribute
{
const string POLICY_PREFIX = "LEMClaim";
public ELocation[] Locations
{
get
{
if (Enum.TryParse(typeof(ELocation[]), Policy.Substring(POLICY_PREFIX.Length), out var locations) )
{
return (ELocation[]) locations;
}
return default(ELocation[]);
}
set
{
int[] intVals = Array.ConvertAll(value, val => (int)val);
string arrayVal = string.Join(",", intVals);
Policy = $"{POLICY_PREFIX}{arrayVal}";
}
}
//remaining code omitted for brevity
}
自定义授权策略提供者:
public class LEMClaimPolicyProvider : IAuthorizationPolicyProvider
{
const string POLICY_PREFIX = "LEMClaim";
public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }
public LEMClaimPolicyProvider(IOptions<AuthorizationOptions> options)
{
FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider(options);
}
public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();
public Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
{
if (!policyName.StartsWith(POLICY_PREFIX, StringComparison.OrdinalIgnoreCase))
return FallbackPolicyProvider.GetPolicyAsync(policyName);
string val = policyName.Substring(POLICY_PREFIX.Length);
//CONVERT STRING TO INT[]
int[] ia = val.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
ELocation[] locations = (ELocation[])(object)ia;
var policy = new AuthorizationPolicyBuilder();
policy.AddRequirements(new LEMClaimRequirement(locations));
return Task.FromResult(policy.Build());
}
}
谢谢您的帮助!
答案 0 :(得分:1)
对于Custom Policy Provider
,它使用LEMClaimAuthorizeAttribute
来构建策略,然后检查生成的策略。为了将多个参数传递给LEMClaimAuthorizeAttribute
,您需要注意生成策略字符串并从策略字符串提取策略的过程。
有关解决方案,请按照以下步骤操作:
LEMClaimAuthorizeAttribute.cs
public class LEMClaimAuthorizeAttribute : AuthorizeAttribute
{
public LEMClaimAuthorizeAttribute(ELocation[] eLocations, EEntity[] eEntities = null)
//public LEMClaimAuthorizeAttribute(ELocation[] eLocations)
{
Locations = eLocations;
Entitys = eEntities;
}
const string POLICY_PREFIX_ELocation = "LEMClaim.ELocation";
const string POLICY_PREFIX_EEntity = "LEMClaim.EEntity";
public ELocation[] Locations
{
get
{
if (Enum.TryParse(typeof(ELocation[]), Policy.Substring(POLICY_PREFIX_ELocation.Length), out var locations))
{
return (ELocation[])locations;
}
return default(ELocation[]);
}
set
{
if (value != null)
{
int[] intVals = Array.ConvertAll(value, val => (int)val);
string arrayVal = string.Join(",", intVals);
Policy = Policy == null ? $"{POLICY_PREFIX_ELocation}{arrayVal}" : Policy + $";{POLICY_PREFIX_ELocation}{arrayVal}";
}
}
}
public EEntity[] Entitys
{
get
{
if (Enum.TryParse(typeof(EEntity[]), Policy.Substring(POLICY_PREFIX_EEntity.Length), out var locations))
{
return (EEntity[])locations;
}
return default(EEntity[]);
}
set
{
if (value != null)
{
int[] intVals = Array.ConvertAll(value, val => (int)val);
string arrayVal = string.Join(",", intVals);
Policy = Policy == null ? $"{POLICY_PREFIX_EEntity}{arrayVal}" : Policy + $";{POLICY_PREFIX_EEntity}{arrayVal}";
}
}
}
//remaining code omitted for brevity
}
LEMClaimRequirement.cs
public class LEMClaimRequirement : IAuthorizationRequirement
{
public LEMClaimRequirement(ELocation[] eLocations, EEntity[] eEntities = null)
{
Locations = eLocations;
Entitys = eEntities;
}
public ELocation[] Locations
{
get; set;
}
public EEntity[] Entitys
{
get; set;
}
}
LEMClaimPolicyProvider.cs
public class LEMClaimPolicyProvider : IAuthorizationPolicyProvider
{
const string POLICY_PREFIX = "LEMClaim";
const string POLICY_PREFIX_ELocation = "LEMClaim.ELocation";
const string POLICY_PREFIX_EEntity = "LEMClaim.EEntity";
public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }
public LEMClaimPolicyProvider(IOptions<AuthorizationOptions> options)
{
FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider(options);
}
public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();
public Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
{
if (!policyName.StartsWith(POLICY_PREFIX, StringComparison.OrdinalIgnoreCase))
return FallbackPolicyProvider.GetPolicyAsync(policyName);
var val = policyName.Split(";");
//get locations
int[] ia1 = val.FirstOrDefault(k => k.StartsWith(POLICY_PREFIX_ELocation, StringComparison.OrdinalIgnoreCase))
.Substring(POLICY_PREFIX_ELocation.Length)
.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
ELocation[] locations = (ELocation[])(object)ia1;
int[] ia2 = val.FirstOrDefault(k => k.StartsWith(POLICY_PREFIX_EEntity, StringComparison.OrdinalIgnoreCase))
?.Substring(POLICY_PREFIX_EEntity.Length)
?.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
EEntity[] entitys = (EEntity[])(object)ia2;
var policy = new AuthorizationPolicyBuilder();
policy.AddRequirements(new LEMClaimRequirement(locations, entitys));
return Task.FromResult(policy.Build());
}
}
使用
[LEMClaimAuthorize(new ELocation[] { ELocation.Indy, ELocation.Columbus })]
public ActionResult One()
{
return View();
}
[LEMClaimAuthorize(new ELocation[] { ELocation.Indy, ELocation.Columbus }, new EEntity[] { EEntity.JobTool })]
public ActionResult Two()
{
return View();
}