MVC ASP.NET(用户角色)中的SignInasync



我在桌面上有以下代码。目前,我正在处理笔记本电脑,并且此代码只是不想工作,它说" Signinasync"在当前上下文中不存在。

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        using (var context = new ApplicationDbContext())
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                var roleStore = new RoleStore<IdentityRole>(context);
                var roleManager = new RoleManager<IdentityRole>(roleStore);
                var userStore = new UserStore<ApplicationUser>(context);
                var userManager = new UserManager<ApplicationUser>(userStore);
                userManager.AddToRole(user.Id, "User");
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
    }

通常,控制器中有一种方法:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() 
    { 
         IsPersistent = isPersistent 
    }, await user.GenerateUserIdentityAsync(UserManager));
}

如果在那里,您可以称呼它。如果不是,我认为您应该添加它。

注意;这应该是帐户控制器中身份模板的一部分。因此,也许您正在使用另一个模板,或者是在另一个控制器中使用此方法。

我最好的猜测是,由于您的代码在台式机上工作而不是在笔记本电脑上工作,因此您没有复制整个项目,而只是复制了代码的某些部分,也许是在不同的模板设置中。

最新更新