2017-11-23 4 views
0

내 데스크톱에서 다음 코드가 작동합니다. 현재 랩톱에서 작업 중이며이 코드는 작동하지 않으므로 "SignInAsync"가 현재 컨텍스트에 존재하지 않는다고 말합니다. 그것은 거기에 당신이 그것을 호출 할 수있는 경우MVC에서 SignInAsync ASP.net (사용자 역할)

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

:

[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); 
     } 
    } 

답변

0

는 일반적으로 컨트롤러의 방법이있다. 그렇지 않다면 추가해야한다고 생각합니다.

참고; 이는 계정 컨트롤러의 ID 템플릿의 일부 여야합니다. 따라서 다른 템플릿을 사용하고 있거나 다른 컨트롤러에서이 방법을 사용 중일 수 있습니다.

코드가 바탕 화면에서 작동하고 랩톱에서 작동하지 않기 때문에 전체 프로젝트를 복사하지 않고 다른 템플릿 설정에서 일부 코드 만 복사했을 것입니다.

관련 문제