有没有办法允许值为空?,("全名+网站(。由于它们是字符串,我不明白为什么它不能为空。有人愿意解释并告诉我如何解决,我遇到的问题吗?
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()">
不幸的是,Claim
不允许插入空值。相反,您可以使用空字符串。