Basically, I'm creating my first ever WCF web service and I'm looking to implement custom authentication and authorization. The authentication seems to be working well, but I want to be able to store roles and permissions using custom authorization as well.
My authentication is done by overriding UserNamePasswordValidator
and making use of the Validate
method.
Validate(string UserName, string password)
Now I've tried implementing the authorization by using the IAuthorizationPolicy
interface
public class AuthorizationPolicy : IAuthorizationPolicy
{
private string _id;
public string Id
{
get { return this._id; }
}
public ClaimSet Issuer
{
get { return ClaimSet.System; }
}
public AuthorizationPolicy()
{
_id = Guid.NewGuid().ToString();
}
public bool Evaluate(EvaluationContext context, ref object state)
{
IIdentity client = GetClientIdentity(context);
context.Properties["Principal"] = new CustomPrincipal(client);
return true;
}
private IIdentity GetClientIdentity(EvaluationContext evaluationContext)
{
object obj;
if (!evaluationContext.Properties.TryGetValue("Identities", out obj))
throw new Exception("No Identity found");
IList<IIdentity> identities = obj as IList<IIdentity>;
if (identities == null || identities.Count <= 0)
throw new Exception("No Identity found");
return identities[0];
}
}
and I've also implemented the CustomPrincipal
using the IPrincipal
interface.
public class CustomPrincipal : IPrincipal
{
IIdentity _identity;
string[] _roles;
public CustomPrincipal(IIdentity identity)
{
_identity = identity;
}
public static CustomPrincipal Current
{
get
{
return Thread.CurrentPrincipal as CustomPrincipal;
}
}
public IIdentity Identity
{
get { return _identity; }
}
public string[] Roles
{
get
{
if (_roles == null)
{
EnsureRoles();
}
return _roles;
}
}
public bool IsInRole(string role)
{
EnsureRoles();
return _roles.Contains(role);
}
protected virtual void EnsureRoles()
{
UserManager userManager = new UserManager();
int userPermissions = userManager.UserPermissions(_identity.Name);
if (userPermissions == 1)
_roles = new string[1] { "ADMIN" };
else
_roles = new string[1] { "USER" };
}
}
My App.Config has been updated as required, and the Evaluate
method in AuthorizationPolicy
is called as expected.
However, this is where I'm stuck. How do I go about implementing the roles and permissions from here?
I would suggest that you opt for a Message Inspector.
The logic would be as follows:
You may refer to Authentication and Authorization with ASP.NET Identity 2.0 for WCF Services
Basically you will have 3 levels to consider according to this article:
System.IdentityModel and System.Security.Principal
Asp.net Identity 2.0 if you want to use the out of the box context shared with MVC applications; or you use your custom database.
Policies if you want fine grained controls of security policy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With