簡體   English   中英

在Identity Custom User之后,User.Identity.GetUserId()將錯誤的類型作為字符串返回

[英]After Identity Custom User , User.Identity.GetUserId() returns wrong type as string

我為實體框架和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; }
    }

GetUserId的定義是這樣的:

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>

因此你應該使用Guid.Parse()Guid.TryParse()

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

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

 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對象的擴展。

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

GetUserId()實際上是以下擴展方法:

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

在某些時候,沒有辦法將值存儲為Guid (cookies!)。 這意味着你必須手動更改它(我今天就自己做了)。 我所做的是將使用Microsoft.AspNet.IdentityMicrosoft.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;
    }
}

只需確保檢查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);
  }
}

所以你必須使用.GetUserId().ToString()來使用某些方法。

對於我們的項目,我需要基於IUserKey抽象密鑰(並將標識符存儲為此接口中的long )。

通過簡單地覆蓋用戶密鑰對象的具體實現中的ToString()方法,解決了(嚴重依賴於字符串的UserManager的問題ToString()

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();
    }
}

在你的情況下,也許你可以在類似的界面中包裝Guid ,然后提供必要的ToString()覆蓋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM