0

I am trying to add authentication and authorization to my API in .NET core 2.1. Everything worked perfectly well. However, when I try to sign In a user, I get this error in my testing environment: "An exception occurred while reading a database value for property 'IdentityUser.Id'. The expected type was 'System.String' but the actual value was of type 'System.Guid'".

I have tried overriding the data type of the id to string but nothing worked for me. I have also tried dropping the tables and recreating and none worked.

ApplicationUser Model:

 public class ApplicationUser : IdentityUser
    {
        public override string Id { get; set; }            
        public string FirstName { get; set; }
        public string LastName { get; set; }       
    }

This is how I am signing the user in:

 public async Task<object> Login ([FromBody] LoginModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                //use signin manager to handle user sign in
                //the error shows up in this method
                var signin = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);
                if (signin.Succeeded)
                {
                    var appuser = _userManager.Users.SingleOrDefault(u => u.Email == model.Email);
                    return await GenerateJwtToken(model.Email, appuser);
                }
            }

            catch(Exception ex)
            {
                return ex.Message;
            }

            throw new ApplicationException("INVALID LOGIN ATTEMPT");                           
           // return BadRequest("no login detail found");
        }

This is my login model

 public class LoginModel
    {
        [Required]
        public string Email { get; set; }
        [Required]
        public string Password { get; set; }
    }

I am aware that the default type of identityuser.id is guid and I want to be able to convert/override it to string so that it conforms with my requirement

Faith
  • 21
  • 8
  • So why don't you change the type of `IdentityUser.Id` to Guid? – thirdDeveloper Oct 22 '19 at 14:24
  • All the property of IdentityUser are all bundled together and it has a type of TKey. I would not want to change it directly so I don't break something. That is why I am looking for a route to do that – Faith Oct 22 '19 at 14:28
  • Try to get it as is (i.e. whatever `_userManager.Users.SingleOrDefault` returns) and then create an ApplicationUser manually from it – thirdDeveloper Oct 22 '19 at 14:33
  • it does not get into that method. it gives me the error on line ```var signin = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);``` – Faith Oct 22 '19 at 14:35
  • The default type is `string`, but the value is actually a GUID. It sounds like someone (you or someone on your team) changed the database column type to a GUID (uniqueidentifier), probably because they thought that nvarchar must have been a mistake, based on the values. – Chris Pratt Oct 22 '19 at 14:36
  • @ChrisPratt Is there a way I can revert that back to let it have data type string? – Faith Oct 22 '19 at 14:39
  • If it is a uniqueidentifier column, you just need to alter the table to make it nvarchar(450) again, as the default would be. Alternatively, you can just actually use `Guid` as the key type for Identity, instead, which is honestly probably easier and better. – Chris Pratt Oct 22 '19 at 14:42
  • I am not sure I know how to do that. any pointers? – Faith Oct 22 '19 at 14:56
  • Check your database and confirm what is type of id in `AspNetUsers` table .If that is Guid , you can change it to nvarchar(450) , if you want to use Guid , refer to code sample :https://stackoverflow.com/questions/37166098/how-to-make-ef-core-use-a-guid-instead-of-string-for-its-id-primary-key – Nan Yu Oct 23 '19 at 03:25

1 Answers1

0

Change your application model class to use the generic IdentityUser type as following:

public class ApplicationUser : IdentityUser<string>
    {
        public override string Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }       
    }
Donald N. Mafa
  • 5,131
  • 10
  • 39
  • 56