5

I am using ASP.Net Identity 2 but soon hope to change to Identity 3 when it becomes more stable (anyone know when that might be?). Here's a sample of my code:

content.ModifiedBy = User.Identity.GetUserId();

The Content table stores ModifedBy as a UNIQUEIDENTIFIER and the Content object assigns a datatype of Guid to ModifiedBy

When I look at the signature for GetUserId() it returns a string.

So how can I take the users UserId and put it into the ModifiedBy which is a Guid?

4

2 回答 2

8

guid 可以将字符串作为构造函数

content.ModifiedBy = new Guid(User.Identity.GetUserId());

于 2015-04-05T17:48:56.080 回答
4

您可以使用 Guid.Parse() 或 Guid.TryParse()

content.ModifiedBy = Guid.Parse(User.Identity.GetUserId());

https://msdn.microsoft.com/en-us/library/system.guid.parse%28v=vs.110%29.aspx

当我一遍又一遍地使用相同的方法时,我添加了以下扩展:

 public static class ExtensionMethods
{
    public static Guid ToGuid(this string value)
    {
        Guid result= Guid.Empty;
        Guid.TryParse(value, out result);
        return result;          
    }
}

然后我用这个:

User.Identity.GetUserId().ToGuid()
于 2016-01-03T18:46:11.920 回答