0

Я создаю приложение для веб-приложений, и Пользователь должен войти в систему с помощью Google или Microsoft. Процесс аутентификации работает нормально, но когда я пытаюсь получить информацию о пользователе, например, адрес электронной почты или первый и второй маты, я всегда получаю нулевой объект. В настоящее время я использую аутентификацию на основе файлов cookie.Asp.Net Core 1.0 Авторизация: не удается получить Userdata от внешнего провайдера входа

Вопрос (ы)

  • Что я не так?
  • Как я могу исправить эту проблему?

Startup.cs:

//*** Services: **** 
//** Workarount to not use EF in Login ** 
//* Source: https://github.com/taherchhabra/AspNetCoreIdentityWithoutEF 
// Add services. 
services.AddApplicationInsightsTelemetry(Configuration); 

services.AddSingleton<IUserStore<EduUserInformation>, Source.UserStore>(); 
services.AddSingleton<IRoleStore<EduUserRole>, Source.RoleStroe>(); 
services.AddIdentity<EduUserInformation, EduUserRole>() 
    .AddDefaultTokenProviders(); 
... 
//**** App: **** 
// Add authentication middleware and inform .NET Core MVC what scheme we'll be using 
services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); 

//Configure Identity Options 
services.Configure<IdentityOptions>(options => 
{ 
    options.Cookies.ApplicationCookie.LoginPath = new Microsoft.AspNetCore.Http.PathString("/Login/Index"); 
}); 
.... 
//Use Identity 
//Source: http://stackoverflow.com/a/34771769 
app.UseIdentity(); 

//Google Authentication configuration options 
app.UseGoogleAuthentication(new GoogleOptions 
{ 
    DisplayName = "Google", 
    AuthenticationScheme = "Google", 
    ClientId = Configuration["Authentication:Google:ClientID"], 
    ClientSecret = Configuration["Authentication:Google:ClientSecret"], 
    Scope = { "openid", "email", "profile" } 
}); 

LoginController.cs:

//POST: /Login/External 
[HttpPost] 
[AllowAnonymous] 
public IActionResult External(string provider, string returnurl = null) 
{ 
    ... 
    //Properties 
    var properties = signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); 

    return new ChallengeResult(provider, properties); 
} 

//GET/POST: /Login/Check 
[HttpGet] 
[HttpPost] 
[AllowAnonymous] 
public async Task<IActionResult> Check(string returnUrl = null, string remoteError = null) 
{ 
    EduModel model = new EduModel(); 
    if (remoteError != null) 
    { 
     logger.LogWarning("Error from External IDP"); 
     return RedirectToAction(nameof(Index)); 
    } 

    //Get Information form Provider 
    var infos = await signInManager.GetExternalLoginInfoAsync(); 

    ... 
} 

Скриншот из переменной "Infos" Screenshot from the Variable "Infos"

EDIT: Cookieoptions в запуске. cs:

//Settings for the Cookie Based Authentication 
public static CookieAuthenticationOptions cookieAuthenticationOptions = new CookieAuthenticationOptions 
{ 
    LoginPath = new Microsoft.AspNetCore.Http.PathString("/Login/Index"), 
    AuthenticationScheme = "Cookies", 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true, 
    AccessDeniedPath = new Microsoft.AspNetCore.Http.PathString("/StatusCode?403"), 
    LogoutPath = new Microsoft.AspNetCore.Http.PathString("/Login/Logout") 
}; 
... 
// Adds a cookie-based authentication middleware to application 
app.UseCookieAuthentication(cookieAuthenticationOptions); 

ответ

0

Я использую метод GetUserAsync в UserManager, чтобы получить Cur пользователя, и это работа для меня, я надеюсь, что вы тоже

ApplicationUser curUser = await _userManager.GetUserAsync(HttpContext.User); 
+0

Я нашел лучшее решение: только с помощью CookeAuth – Yannik

 Смежные вопросы

  • Нет связанных вопросов^_^