c# - MVC custom roles based off of windows authenticated credentials -
i have mvc 5 web applications authenticating users via windows authentication. goal implement custom roles based on windows authenticatied username. have seen atricles explain this, build application on desgin of user roles, not windows autnetication.
my issues is, how can add custom roles based off windows authenticated account?
one approach id add custom filters restricted controllers in order validate access:
[haspermission(enumsecuredfunctionality.accounts)] public class account : controller { }
inside custom filter can handle permissions:
public class haspermission : actionfilterattribute { public enumsecuredfunctionality[] actions { get; set; } public haspermission(params enumsecuredfunctionality[] actions) //for example can pass secured functionalities restrict { actions = actions; } public override void onactionexecuting(actionexecutingcontext filtercontext) { basecontroller controller = (basecontroller)filtercontext.controller; userauthdata userauthdata = (userauthdata)controller.viewbag.userauthdata; if (!controller.userisauthenticated || !controller.validatepermission(userauthdata.shopseller.loginname.tostring(), actions[0])) { //redirect routevaluedictionary redirecttargetdictionary = new routevaluedictionary(); redirecttargetdictionary.add("action", "showmessage"); redirecttargetdictionary.add("controller", "error"); redirecttargetdictionary.add("message", "role " + enumtostring.enumroletostring(userauthdata.shopseller.enumrole) + " no permissions"); filtercontext.result = new redirecttorouteresult(redirecttargetdictionary); } } }
Comments
Post a Comment