Question

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.

Was it helpful?

Solution

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();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top