名称不能为null或为空. ASP.NET身份MVC 5 + EntityFreamwork [英] Name cannot be null or empty. asp.net identity mvc 5 + EntityFreamwork

查看:114
本文介绍了名称不能为null或为空. ASP.NET身份MVC 5 + EntityFreamwork的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对此有疑问吗?他向我抛出错误:名称不能为null或为空.但是在表中没有名为名称"的属性.我要自定义Asp.Net身份.

Has anyone had a problem with this. He throws me the error: Name can not be null or empty. But in the table do not have the attribute called "Name". I want to customize Asp.Net Identity.

控制器

  public class HomeController : Controller
{
    //
    // GET: /Home/
    public async Task<ActionResult> Index()
    {
        var userStore = new UserStore<User,Identity_2Context.MyRole,long,Identity_2Context.MyUserLogin,Identity_2Context.MyUserRole,Identity_2Context.MyClaim>(new Identity_2Context());
        var manager = new UserManager<User,long>(userStore);

        var user = new User()
        {
            UserName = "TehnoMac",Email = "TehnoMac@tehcno.com",IsApproved = true,IsLockedOut = false,
            CreatedDate = DateTime.Now,LastActivityDate = DateTime.Now,LastLoginDate = DateTime.Now,FailedPasswordAnswerAttemptCount = 0,
            FailedPasswordAttemptCount = 0
        };

        var result = await manager.CreateAsync(user, "test123test");

        if (result.Succeeded)
        {
            var authenticationManager = HttpContext.GetOwinContext().Authentication;
            var userIdentity = manager.Create(user, DefaultAuthenticationTypes.ApplicationCookie);
        }
        else
        {
            ViewBag.Text = result.Errors.FirstOrDefault();
        }

        return View();
    }
}

身份模型 公共类ApplicationUser:IdentityUser { }

IdentityModel public class ApplicationUser : IdentityUser { }

public class MyClaim:IdentityUserClaim<long>
{
}

public class MyRole:IdentityRole<long,MyUserRole>
{
}

public class MyUserRole:IdentityUserRole<long>
{
}
public class MyUserLogin:IdentityUserLogin<long>
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,MyRole,long,MyUserLogin,MyUserRole,MyClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

推荐答案

确保已指定用户名.我遇到了同样的错误,设置了UserName属性后,该错误消失了.

Make sure you have specified a username. I got the same error and after setting the UserName property, the error went away.

这篇关于名称不能为null或为空. ASP.NET身份MVC 5 + EntityFreamwork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆