Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Identity 2.1 alpha SignInManager.GetVerifiedUserIdAsync invalid cast

I'm using ASP.Net MVC 5 with ASP.Net Identity 2.1 alpha in my project.

For identity I have implemented the User Manager to use a Guid as key.

I'm implementing Two factor authentication. When I try to get the user id

var userId = await SignInManager.GetVerifiedUserIdAsync();

I get an error

Invalid cast from 'System.String' to 'System.Guid'.

Is it bug with Identity alpha release or I'm missing something? If I use one factor authentication all is good. Users are created in database you can change user data etc.

Any suggestion will be appreciated. Thank you.

like image 889
The Tech Geek Avatar asked Jul 30 '14 09:07

The Tech Geek


1 Answers

I have found the solution. Looking in the definition of SignInManager I noticed two methods

public virtual TKey ConvertIdFromString(string id);
public virtual string ConvertIdToString(TKey id);

So i just override these two methods in my implementation of SignInManager and everything worked.

    public override Guid ConvertIdFromString(string id)
    {
        if (string.IsNullOrEmpty(id)) return Guid.Empty;

        return new Guid(id);
    }

    public override string ConvertIdToString(Guid id)
    {
        if (id.Equals(Guid.Empty)) return string.Empty;

        return id.ToString();
    }
like image 159
The Tech Geek Avatar answered Sep 17 '22 05:09

The Tech Geek