---
title: "cast the value returned from User.Identity.GetUserId() to make it a GUID"  
description: "cast the value returned from User.Identity.GetUserId() to make it a GUID"  
author: "Mark Devid"  
published: 2014-11-14  
updated: 2014-11-14  
canonical: https://www.mindstick.com/forum/12585/cast-the-value-returned-from-user-identity-getuserid-to-make-it-a-guid  
category: "asp.net"  
tags: ["c#"]  
reading_time: 1 minute  

---

# cast the value returned from User.Identity.GetUserId() to make it a GUID

I created the following [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp):

```
public class Config{    public Guid UserId { get; set; }    public string AdminJSON { get; set; }    public string UserJSON { get; set; }}
```

When I [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) the data I am using:

[Config](https://www.mindstick.com/forum/55094/what-is-machine-config-in-asp-dot-net) 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](https://www.mindstick.com/articles/1527/string-split-in-c-sharp). Should I be casting this to return a GUID.

I also have another [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) 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](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) saying [Error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) 1 [Cannot convert](https://www.mindstick.com/forum/12809/error-cannot-convert-from-object-to-string-when-filling-a-asp-dropdownlist-dynamically) type 'string' to 'System.Guid'

It [errors](https://answers.mindstick.com/qa/116170/fresh-fir-against-gandhis-in-national-herald-case-cover-up-for-ed-s-own-errors) out as it cannot

## Replies

### Reply by Anonymous User

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"); }
```


---

Original Source: https://www.mindstick.com/forum/12585/cast-the-value-returned-from-user-identity-getuserid-to-make-it-a-guid

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
