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? -

java - How to create Table using Apache PDFBox -

mpi - Why is MPI_Bsend not returning error even when the buffer is insufficient to accommodate all the messages -