在Identity Custom User之后,User.Identity.GetUserId()返回错误的类型作为字符串 [英] After Identity Custom User , User.Identity.GetUserId() returns wrong type as string
问题描述
我为实体框架和asp.net身份创建了自定义用户模型. 我在这里检查了其他答案,它们与我的问题很接近,但是答案不符合我的要求.所以我在这里问.
I created custom User model for entity framework and asp.net identity. I checked other answers in here, they are close to my question but answers not apply my requirement. so i am asking here.
一切都很好. 但是当我尝试获取用户ID时:
Everything is ok. But when i try to get user id like :
ApplicationUser user = await manager.FindByIdAsync(User.Identity.GetUserId());
User.Identity.GetUserId()返回字符串.我像Guid一样配置它.但它返回字符串:(
User.Identity.GetUserId() return string. I configured it like Guid. But it returns string :(
我的课在这里. 请帮助我,如何在Web api控制器中以Guid的形式获取User.Identity.GetUserId()?
My classes are here. Please help me, how can i get User.Identity.GetUserId() in a web api 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; }
}
推荐答案
GetUserId的定义是:
The definition of GetUserId is this:
public static string GetUserId(this IIdentity identity);
public static T GetUserId<T>(this IIdentity identity) where T : IConvertible;
很遗憾,Guid不是IConvertible,并且不会以通用形式返回Guid类型.因此,我们不能使用:User.Identity.GetUserId()<Guid>
,因为某些人在将ID更改为Int时会使用User.Identity.GetUserId()<Int>
.
Unfortunately Guid is not an IConvertible and it will not return the Guid type in the Generic form. Therefore, we can not use: User.Identity.GetUserId()<Guid>
as some people use User.Identity.GetUserId()<Int>
when they change the ID to Int.
因此,您应该使用Guid.Parse()
或Guid.TryParse()
Guid.Parse(User.Identity.GetUserId())
@Erik方法工作正常,但是由于我不想覆盖原始方法,所以添加了以下常规扩展名:
@Erik method works fine, But As I did not wanted to override the original one, 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;
}
}
然后我用这个:
User.Identity.GetUserId().ToGuid()
我想出了另一种解决方案:
我向Identity对象添加了扩展名.
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;
}
}
这篇关于在Identity Custom User之后,User.Identity.GetUserId()返回错误的类型作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!