asp.net mvc 4 - SimpleMembershipProvider roles not accessible -


i have mvc4 application uses simplemembershipprovider authentication mechanism.

everything works fine, apart of when return application , authenticate using persistant cookie.

i authenticated fine, cannot access roles assigned to. effectively, cannot access roles @ all:

string.join(",", roles.getrolesforuser(user.identity.name))  

returns empty string

what might causing that?

this can happen when simplemembershipprovider hasn't been initialized. example mvc forms authentication template assumes you'll allowing anonymous access site , doesn’t initialize membership provider until go login page. however, more common security technique require login site access , define menu choices in _layout page determined roles. but, if use persistent cookie, don’t revisit login page roles authenticated user aren’t loaded membership database.

what want initialize provider when user enters site values loaded. this, want add following filter in registerglobalfilters method of filterconfig class in app_start folder

filters.add(new yourappnamespace.filters.initializesimplemembershipattribute()); 

this cause user data loaded database when cookie authenticated user enters site.

another alternative technique add [initializesimplemembership] decorator controller method cookie autheticated users might enter directly. kind of messy though if have put on lot of controllers. therefore, putting in global filter better in cases.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -