4

I am implementing authorisation on a web application using the ASP.NET SqlMembershipProvider, but it is randomly throwing an invalid cast exception when calling the GetUser method.

I can log in, and navigate the pages all fine, but than at random points, boom, it craps out, and throws the exception:

Unable to cast object of type 'System.Int32' to type 'System.String'

Looking at the stack trace you can clearly see it is being caused by the Membership Provider:

[InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.]
   System.Data.SqlClient.SqlBuffer.get_String() +5002910
   System.Data.SqlClient.SqlDataReader.GetString(Int32 i) +55
   System.Web.Security.SqlMembershipProvider.GetUser(String username, Boolean userIsOnline) +1169
   System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline) +63
   System.Web.Security.Membership.GetUser() +19
   _Default.LoadLeadOverview() in \\file1\default.aspx.vb:169
   _Default.Page_Load(Object sender, EventArgs e) in \\file1\default.aspx.vb:30
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

I have had a look around and found other people with the same issue, but no solution. Any thoughts would be welcome.

Ira Rainey
  • 5,173
  • 2
  • 34
  • 42

2 Answers2

1

Whilst I haven't done anything to specifically resolve this issue, I haven't seen it in a couple of weeks. Looking back, I was testing the site in multiple browsers at the same time, with the same account, logging in and out a lot, and that seemed to be when then problem came up. Now all the CSS and other cross-browser stuff has been settled, I'm not using multi browsers so heavily, and not at the same time and it seems to be fine.

Ira Rainey
  • 5,173
  • 2
  • 34
  • 42
0

Is it possible some of your users have all digits as their username? It might be that auto type is boxing wrong somewhere along the way.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • No, I only have two user accounts at the moment whilst testing/developing, and they are both just alpha chars plus and underscore. – Ira Rainey Apr 19 '11 at 08:55
  • I've had the same problem when I've taken all the code out of default.aspx. It appears to be caused by the membership provider, which is a bit of a concern. The problem is it appears to random and inconsistent, which makes it very hard to pin down the cause. All I know is that the only consistent factor is the exception appears to be thrown by the membership provider itself. – Ira Rainey Apr 19 '11 at 11:15
  • @Ira - this is not what your stack trace shows. Please show the SQL sent to the db server or the content of the SP on the server. – Hogan Apr 19 '11 at 12:05
  • No, you're right, that stack trace was taken from a page that had code in it. However, I then later tried taking the code out and the same problem was coming up. This is following a redirect from a login page defined in the web.config file, which defines the authentication mode as mode="Forms" and restricts access to all other pages using . I haven't seen the exception for a few days, but if it comes up again I will post the SQL. Thanks for the help. – Ira Rainey Apr 19 '11 at 13:19