Friday, March 1, 2013

The method Roles.GetRolesForUser throws an error

Exception of type 'System.ArgumentException' was thrown.
Parameter name: encodedValue


Below post helped me understand cause of the error, it was resolved after attaching 0#
.w|<Domain Name>\\ to the user name
 
string userIdentityName = "0#.w|<domain name>\\" + username;


Reference

http://go4answers.webhost4life.com/Example/custom-role-provider-138386.aspx


Ironically Roles.AddUserToRole(userName, roleName) does not need this modification and it can work only with the user name, thus finally though first statement is working, second is still failing because user in GetRolesForUser is different that the user in AddUserToRole method.

After some efforts I found out that solution is to use

string[] usersinrole = Roles.GetUsersInRole(roleName);
or
 bool isuserinrole = Roles.IsUserInRole(userName, roleName);
 
these two methods return the same user as AddUserToRole method.

I need to check if below solution works.
http://blog.mastykarz.nl/notetoself-custom-membership-role-providers-sharepoint-2010-claims/

No comments:

Regex Email validation in c# dot net core

 Use this regex /^_?[a-zA-Z0-9]([a-zA-Z0-9]*[._+-])*[a-zA-Z0-9_]+@(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.(?!-)[A-Za-z0-9-]{1,63}(?<!-))*\.[A-...