2017-02-19 22 views
0

我們如何從帳戶控制器中訪問ApplicationUser的屬性。在用戶創建ApplicationUser對象的同時,將對象引用設置爲null錯誤。示例代碼Facebook或其他社交媒體登錄Ap.net中的ApplicationUser對象核心

public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null) 
    { 
     if (remoteError != null) 
     { 
      ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}"); 
      return View(nameof(Login)); 
     } 
     var info = await _signInManager.GetExternalLoginInfoAsync(); 
     if (info == null) 
     { 
      return RedirectToAction(nameof(Login)); 
     } 

     // Sign in the user with this external login provider if the user already has a login. 
     var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false); 
     if (result.Succeeded) 
     { 
      _logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider); 

      //I want to access the ApplicationUser Properites here 

      return RedirectToLocal(returnUrl); 
     } 
     if (result.RequiresTwoFactor) 
     { 
      return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl }); 
     } 
     if (result.IsLockedOut) 
     { 
      return View("Lockout"); 
     } 
     else 
     { 
      // If the user does not have an account, then ask the user to create an account. 
      ViewData["ReturnUrl"] = returnUrl; 
      ViewData["LoginProvider"] = info.LoginProvider; 
      var email = info.Principal.FindFirstValue(ClaimTypes.Email); 
      return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email }); 
     } 
    } 

回答

1

您應該能夠訪問ApplicationUser這樣的...

//I want to access the ApplicationUser Properites here 
var user = await _userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey); 
+0

感謝很多人 –

+0

歡迎您:-) –