0

I’m building a Webapplication and the User should login with Google or Microsoft. The Authenticationprocess working fine but when I try to get Information about the User like an Email-Address or First- and Secondmame I always get a Null-Object. Currently I use a Cookie based Authentication.

Question(s)

  • What do I wrong?
  • How can I fix this Issue?

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();

    ...
}

Screenshot from the Variable "Infos" Screenshot from the Variable "Infos"

EDIT: Cookieoptions in Startup.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);
Yannik
  • 887
  • 3
  • 9
  • 29

1 Answers1

0

I use GetUserAsync method in UserManager to get cur user and it's work for me, i hope you too

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