簡體   English   中英

ASP.NET Identity CreateAsync返回錯誤“名稱不能為null或為空。”

[英]ASP.NET Identity CreateAsync returning Error “Name cannot be null or empty.”

我已經嘗試了所有可以找到的答案。 我正在嘗試將身份添加到我的ASP.NET MVC 5項目中。 我首先使用數據庫。 已經存在一個數據庫。 我使用來自該GitHub的腳本在數據庫中創建身份表。

https://gist.github.com/jeroenheijmans/8fa79427abc25a864cb055616644172f

這是我的Startup.cs文件

public void Configuration(IAppBuilder app)
    {

        app.CreatePerOwinContext(() => new AppDbContext());
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<RoleManager<AspNetRole>>((options, context) =>
          new RoleManager<AspNetRole>(
              new RoleStore<AspNetRole>(context.Get<AppDbContext>())));


        app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions()
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")


        });

    }

這是我的ApplicationUserManager:

public void Configuration(IAppBuilder app)
    {

        app.CreatePerOwinContext(() => new AppDbContext());
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<RoleManager<AspNetRole>>((options, context) =>
          new RoleManager<AspNetRole>(
              new RoleStore<AspNetRole>(context.Get<AppDbContext>())));


        app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions()
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")


        });

    }

這是我的注冊動作:

 public async Task<ActionResult> Register()
        {
            var model = (RegisterUserModel)Session["RegisterAccount"];
            var user = new AspNetUser()
            {
                UserName = model.Email,
                Email = model.Email
            };
            var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            IdentityResult addUserResult = await userManager.CreateAsync(user, model.Password);
}

我的角色班

public partial class AspNetRole : IdentityRole
    {
        public AspNetRole(string name) { Name = name; }
    }

我的用戶類別

 public partial class AspNetUser : IdentityUser
{

}

我的DbContext

         public partial class AppDbContext: IdentityDbContext<AspNetUser>
{
        public AppDbContext(string connectionString)
            : base(connectionString)
        {}
    }

檢查model.Email值。 似乎它為空/空。 為了確保這一點,請將Register方法放入try-catch塊中,並嘗試提取有關錯誤/異常的更多詳細信息:

public async Task<ActionResult> Register()
{
  try
  {
    var model = (RegisterUserModel)Session["RegisterAccount"];
    var user = new AspNetUser()
    {
      UserName = model.Email,
      Email = model.Email
    };
    var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
    IdentityResult addUserResult = await userManager.CreateAsync(user, model.Password);

    if (!addUserResult.Succeeded)
    {
      //loop over addUserResult.Errors for more detail
    }
  }
  catch (Exception exception)
  {
    //here, check the exception for more details
  }
}

暫無
暫無

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

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