I am searching for a good approach to automatically check the access rights when accessing a propertys getter and setter.
This is just for curiosity and experimental purposes, so performance does not really matter.
One Approach I found is the following (example is just for getters):
public class DynamicPropertyClass : DynamicObject
{
/// <summary>
/// I want this to work for a default auto-property
/// </summary>
public string TestString { get; set; }
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (StaticSecurityContext.CheckSecurityConditionHere())
{
return base.TryGetMember(binder, out result);
}
else
{
throw new SecurityException();
}
}
}
Using a dynamic object to abort if the Security Conditions are not given. The problem with this approach is that properties that actually exist on the class won't get handled by the TryGetMember method. So this approach forces me to only handle properties that do not actually exist in code.
As you can see the dynamic capabilitys are not really relevant for my approach. I want something similar to work on my properties written in code, dynamic capabilitys do not have to be supported.
So, is there another approach, possible in c#, without applying attributes on every property or adding custom code to the getters and setters?
Aucun commentaire:
Enregistrer un commentaire