在Identity Custom User之后,User.Identity.GetUserId()将错误的类型作为字符串返回

Ras*_*Ras 6 c# asp.net-identity-2

我为实体框架和asp.net身份创建了自定义用户模型.我在这里检查了其他答案,他们接近我的问题,但答案不符合我的要求.所以我在这里问.

一切都好.但是当我尝试获得用户ID时:

 ApplicationUser user = await manager.FindByIdAsync(User.Identity.GetUserId());
Run Code Online (Sandbox Code Playgroud)

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; }
    }
Run Code Online (Sandbox Code Playgroud)

Ami*_*mir 10

GetUserId的定义是这样的:

public static string GetUserId(this IIdentity identity);
public static T GetUserId<T>(this IIdentity identity) where T : IConvertible;
Run Code Online (Sandbox Code Playgroud)

不幸的是,Guid不是IConvertible,它不会返回通用形式的Guid类型.因此,我们不能使用:User.Identity.GetUserId()<Guid>因为有些人User.Identity.GetUserId()<Int>在将ID更改为Int时使用.

因此你应该使用Guid.Parse()Guid.TryParse()

Guid.Parse(User.Identity.GetUserId())
Run Code Online (Sandbox Code Playgroud)

@Erik方法工作正常,但由于我不想覆盖原始方法,我添加了以下一般扩展:

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

然后我用这个:

User.Identity.GetUserId().ToGuid()
Run Code Online (Sandbox Code Playgroud)

编辑:我提出了另一个解决方案:

我添加了Identity对象的扩展.

public static class IdentityExtensions
{
    public static Guid GetGuidUserId(this IIdentity identity)
    {
        Guid result = Guid.Empty;
        Guid.TryParse(identity.GetUserId(), out result);
        return result;
    }   
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*ips 5

GetUserId() 实际上是以下扩展方法:

Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(this IIdentity identity)
Run Code Online (Sandbox Code Playgroud)

在某些时候,没有办法将值存储为Guid(cookies!).这意味着你必须手动更改它(我今天就自己做了).我所做的是移动使用的一切Microsoft.AspNet.Identity,Microsoft.AspNet.Identity.OwinMicrosoft.Owin.Security成一个单独的目录.然后写了我自己的扩展类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

只需确保检查Guid.Empty(或将其更改为Guid?).你会发现有很多东西可以接受string,你必须转换它们.例如:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,您必须使用.GetUserId().ToString()某些方法才能工作.