Home > DeveloperSection > Forums > cast the value returned from User.Identity.GetUserId() to make it a GUID
Mark Devid
Mark Devid

Total Post:101

Points:707
Posted on    November-14-2014 2:29 AM

 ASP.Net C# 
Ratings:


 1 Reply(s)
 1864  View(s)
Rate this:

I created the following class:

public class Config

{

    public Guid UserId { get; set; }

    public string AdminJSON { get; set; }

    public string UserJSON { get; set; }

}

When I query the data I am using:

Config config = await db.Configs.FindAsync(User.Identity.GetUserId());

Is this the correct way to do the find. It seems that User.Identity.GetUserId() returns a string. Should I be casting this to return a GUID.

I also have another problem with this:

UserId = User.Identity.GetUserId()  

This also fails. I tried to do a cast by adding (Guid) before the User.Identity.GetUserId() however this gives a message saying Error 1 Cannot convert type 'string' to 'System.Guid'

It errors out as it cannot



jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    November-14-2014 6:21 AM

You need to use Guid.Parse(User.Identity.GetUserId()) or better fail proof method

Guid userId;

bool worked=Guid.TryParse(User.Identity.GetUserId(),out userId);

if(worked)

{

    //go ahead

}

else

{

    throw new Exception("Invalid userid");

}


Don't want to miss updates? Please click the below button!

Follow MindStick