0

Using Identity 2.0 with a Web Forms project.

I can login successfully, and control access to pages using <allow users="<emailaddress>"/>, but when I add a user to a role in AspNetUserRoles, it causes the application to return this error:

The specified cast from a materialized 'system.int32' type to the 'system.string' type is not valid

The string of code that Visual Studio references on the crash is:

var user = manager.FindByName(Email.Text)

How can I fix this?

LJNielsenDk
  • 1,414
  • 1
  • 16
  • 32
Rhyss
  • 1
  • 1
  • 1
    step into the .FIndByName function and see what part is trying to cast an int into a string – Ryan McDonough Apr 11 '15 at 21:18
  • If can not step into the `FindByName` method, you can use ILSpy to see what happened in the method. – zhimin Apr 11 '15 at 22:04
  • I've gone through the options, and everything points to the FindByName method needing a string only. Setting a break point give me info on the value coming in, and it is a string, so it's not the text field doing something strange. And the fact it only effects users who exist in the AspNetUserRoles table is another conundrum. – Rhyss Apr 12 '15 at 03:32

1 Answers1

0

Discovered the problem. Our DB editor had changed the AspNetRoles table so that the RoleID field would auto-increment, and in doing so, the field changed from an NVARCHAR(256) to an INT. Changed it back and the problems has been resolved.

Rhyss
  • 1
  • 1
  • I dunno if I'd recommend that you use this workaround. There is a pretty huge difference between an NVARCHAR and an INT, specifically in terms of data usage and search-ability. You should probably look into fixing your code so that it supports a proper ID field – Dave Lasley Apr 12 '15 at 17:58
  • It's not a workaround, it's returning the database to the original settings. We moved it from an NVARCHAR to an INT when we started so that we could autoincrement it, but the list is static so we don't need to adjust it often. And there are also methods built into Identity 2.0 that will allow us to create roles via the front end if we need to. – Rhyss Apr 13 '15 at 06:24
  • There's a reason that database best practices say to always use INT and autoincrement for IDs, but it's your prerogative as to whether you want to follow best practices I guess. – Dave Lasley Apr 13 '15 at 19:57