After the user user, Identity User.Identity.GetUserId () returns the wrong type as a string

I created a custom model for the entity structure and asp.net identifier. I checked the other answers here, they are close to my question, but the answers do not meet my requirements. so I ask here.

Everything is good. But when I try to get the user id, for example:

ApplicationUser user = await manager.FindByIdAsync(User.Identity.GetUserId()); 

User.Identity.GetUserId () returns a string. I configured it as a guid. But it returns a string: (

My classes are here. Please help me, how can I get User.Identity.GetUserId () in the api web controller as Guid?

 public class GuidUserLogin : IdentityUserLogin<Guid> { } public class GuidUserRole : IdentityUserRole<Guid> { } public class GuidUserClaim : IdentityUserClaim<Guid> { } public class GuidRole : IdentityRole<Guid, GuidUserRole> { } //public class GuidUserContext : IdentityDbContext<ApplicationUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { } public class GuidUserStore : UserStore<ApplicationUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { public GuidUserStore(DbContext context) : base(context) { } } public class GuidRoleStore : RoleStore<GuidRole, Guid, GuidUserRole> { public GuidRoleStore(DbContext context) : base(context) { } } public class ApplicationUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>, ICreateDateRequired { public ApplicationUser() { } [Key] public Guid UserId { get; set; } public override Guid Id { get; set; } public override string UserName { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Password { get; set; } //Matching Attributes public override string Email { get; set; } public string FacebookId { get; set; } public string TwitterId { get; set; } public string GoogleId { get; set; } public override string PhoneNumber { get; set; } public string SocialAccessToken { get; set; } public string Gender { get; set; } public virtual List<Shade> Shades { get; set; } [InverseProperty("ParentUser")] public virtual List<UserFriendship> Friends { get; set; } public virtual List<Contact> Contacts { get; set; } //[Column(TypeName = "DateTime2")] //[DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime? CreateDate { get; set; } public string ImageUrl { get; set; } public string ThumbUrl { get; set; } public string BackUrl { get; set; } public virtual List<ContactDetail> UserContactDetails { get; set; } } 
+6
source share
3 answers

The definition of GetUserId is as follows:

 public static string GetUserId(this IIdentity identity); public static T GetUserId<T>(this IIdentity identity) where T : IConvertible; 

Unfortunately, Guid is not IConvertible, and it will not return a Guid type in Generic form. Therefore, we cannot use: User.Identity.GetUserId()<Guid> , as some people use User.Identity.GetUserId()<Int> when they change ID to Int.

Therefore you should use Guid.Parse() or Guid.TryParse()

 Guid.Parse(User.Identity.GetUserId()) 

The @Erik method works fine, but since I did not want to override the original, I added the following general extension:

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

and then I used this:

 User.Identity.GetUserId().ToGuid() 

Edit: I came up with another solution:

I added an extension to the Identity object.

 public static class IdentityExtensions { public static Guid GetGuidUserId(this IIdentity identity) { Guid result = Guid.Empty; Guid.TryParse(identity.GetUserId(), out result); return result; } } 
+9
source

GetUserId() is actually the following extension method:

 Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(this IIdentity identity) 

At some point it is not possible to save the value as a Guid (cookie!). This means that you have to manually change it (I just did it myself today). I did a move of everything that uses Microsoft.AspNet.Identity , Microsoft.AspNet.Identity.Owin and Microsoft.Owin.Security into a separate directory. Then he wrote his own extension class:

 internal static class IIdentityExtensions { public static Guid GetUserId(this IIdentity identity) { var result = Guid.Empty; string id = Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(identity); Guid.TryParse(id, out result); return result; } public static string GetUserName(this IIdentity identity) { string result = Microsoft.AspNet.Identity.IdentityExtensions.GetUserName(identity); return result; } public static string FindFirstValue(this ClaimsIdentity identity, string claimType) { string result = Microsoft.AspNet.Identity.IdentityExtensions.FindFirstValue(identity, claimType); return result; } } 

Just make sure you check Guid.Empty (or change it to Guid?). You will find that there are many things that simply accept string , and you will have to convert them. For instance:

 namespace Microsoft.Owin.Security { public static class AuthenticationManagerExtensions { public static ClaimsIdentity CreateTwoFactorRememberBrowserIdentity( this IAuthenticationManager manager, string userId); public static Task<bool> TwoFactorBrowserRememberedAsync( this IAuthenticationManager manager, string userId); } } 

So you have to .GetUserId().ToString() use certain methods.

+5
source

For our project, I needed to separate the key based on IUserKey (and save the identifier as long in this interface).

The issue with the UserManager highly string dependent was resolved (for me) by simply overriding the ToString() method in the specific implementation of the user key object.

 public class IdentityUserKey : IIdentityUserKey { public long UserId { get; set; } public bool Equals(IIdentityUserKey other) { return other != null && other.UserId == UserId; } public override string ToString() { return UserId.ToString(); } } 

In your case, perhaps you could wrap Guid in a similar interface, and then provide the necessary ToString() override.

0
source

All Articles