我为实体框架和 asp.net 身份创建了自定义用户模型。我在这里检查了其他答案,它们与我的问题很接近,但答案不符合我的要求。所以我在这里问。
一切都好。但是当我尝试获取用户 ID 时:
ApplicationUser user = await manager.FindByIdAsync(User.Identity.GetUserId());
User.Identity.GetUserId() 返回字符串。我像Guid一样配置它。但它返回字符串:(
我的课在这里。请帮助我,如何在 Web api 控制器中获取 User.Identity.GetUserId() 作为 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; }
}