ProviderException: InvalidCastException

447 Views Asked by At

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

1

There are 1 best solutions below

1
On

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())