userIdentity.AddClaim值不能为null

时间:2018-01-24 07:45:23

标签: c# asp.net-mvc model asp.net-identity

有没有办法允许值为空?,(" FullName + Website)。因为它们是字符串,所以我不明白为什么它不能为空。任何人都在关心解释并告诉我如何解决,我有这个问题吗?

 public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }
    public string Website { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        userIdentity.AddClaim(new Claim("FullName", FullName));
        userIdentity.AddClaim(new Claim("Website", Website));
        return userIdentity;
    }

}

这是我使用数据的方式。

 public static class IdentityExtensions
{
    public static string GetFullName(this IIdentity identity)
    {
        if (identity == null)
        {
            throw new ArgumentNullException("identity");
        }
        var ci = identity as ClaimsIdentity;
        if (ci != null)
        {
            return ci.FindFirstValue("FullName");
        }
        return null;
    }

    public static string GetWebsite(this IIdentity identity)
    {
        if (identity == null)
        {
            throw new ArgumentNullException("identity");
        }
        var ci = identity as ClaimsIdentity;
        if (ci != null)
        {
            return ci.FindFirstValue("Website");
        }
        return null;
    }

}

剃刀:

 <input class="form-control" value="@User.Identity.GetFullName()">

1 个答案:

答案 0 :(得分:1)

声明deos不允许空值,遗憾的是你可以使用字符串空(“”)。