views:

111

answers:

1

Few of our clients are regularly getting invalid cast exception, with variations i.e. InvalidCastException / ProviderException, but both generating from method call: System.Web.Security.SqlRoleProvider.GetRolesForUser(String username)

The other variation is:

Exception type: InvalidCastException
Exception message: Unable to cast object of type System.Int32 to type System.String.

I had a look at application event log which shows:

Stack trace:
   at System.Web.Security.SqlRoleProvider.GetRolesForUser(String username)
   at System.Web.Security.RolePrincipal.IsInRole(String role)
   at System.Web.Configuration.AuthorizationRule.IsTheUserInAnyRole(StringCollection roles, IPrincipal principal)
   at System.Web.Configuration.AuthorizationRule.IsUserAllowed(IPrincipal user, String verb)
   at System.Web.Configuration.AuthorizationRuleCollection.IsUserAllowed(IPrincipal user, String verb)
   at System.Web.Security.UrlAuthorizationModule.OnEnter(Object source, EventArgs eventArgs)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)*

Has anyone come across this issue, and if so what is the fix?

Thanks

JS

A: 

The error message is telling you that you are attempting to cast (read convert) from an integer type to a string type and that that cast was not successful. This occurred in the GetRolesForUser(String) function. So perhaps you are passing an integer in instead of a string and it is being parsed incorrectly. Either way we'll need more code to be sure. I hope this helps.

Try making sure that whenever you give an integer to something that requires a string pass call the toString() function on it first. For example:

GetRolesForUser(someInt32Var.toString())
Robert Massaioli
Shhnapthanks for your reply,i would love to pass the right parameter, but as obvious from the stack trace, this seems to be happening because of the way asp .net handles membership internally.Also interesting thing to note is that this error happens not so very often, possibly once per day or some times it happens at each login attempt. It is hard to reproduce consistently, making it even more trickier to fix. Providing further information this happens when a call is made to this methodMembership.GetUser(Login.UserName);
JS