c# - Custom Authorize Attribute and Entity Framework -
i implementing action/activity based authorization in asp.net application. trying make reusable library can use other projects. using entity framework data access.
i able implement functionality not sure how configure entity framework connection string authorize attribute.
custom authorize attribute:
public class authorizeaction: authorizeattribute { private string _actions; private string[] _actionssplit = new string[0]; public string actions { { return _actions ?? string.empty; } set { _actions = value; _actionssplit = value.split(','); } } protected override bool authorizecore(system.web.httpcontextbase httpcontext) { string userid = string.empty; if (httpcontext == null) { throw new argumentnullexception("httpcontext"); } iprincipal user = httpcontext.user; if(!user.identity.isauthenticated) { return false; } userid = user.identity.name; if(_actionssplit.length > 0) { authmanager manager = new authmanager(); if (!manager.authorizeaction(_actionssplit, userid)) //this authorize user against action return false; } return true; }
authmanager class using has overload method accept entity framework connection string name configuration file not sure how can configurable or user supplied.
authmanager code:
public class authmanager { private string connectionconfigname = "authorizationcontext"; public authmanager() { } public authmanager(string connectionname) { connectionconfigname = connectionname; } /// <summary> /// authorizes actions against userid supplied /// </summary> /// <param name="actionnames">comma seperated list of action names</param> /// <param name="useralias">unique user identifier</param> /// <returns>true if user has access atleast 1 of actions supplies otherwise false</returns> public bool authorizeaction(string[] actions, string useralias) { using (authorizationcontext context = new authorizationcontext(connectionconfigname)) { //list<string> actions = actionnames.split(',').tolist<string>(); var count = (from ur in context.userroles join ra in context.roleactions on new { key1 = ur.roleid, key2 = ur.useralias } equals new { key1 = ra.roleid, key2 = useralias} join in context.actions on ra.actionid equals a.actionid actions.contains(a.actionname) && ra.allow == true select ur).count(); return (count > 0); } } }
i looked @ using roleprovider not going use of built in functionality not sure if idea. please help.
Comments
Post a Comment