Asp.Net Core 1.0授权:无法从外部登录提供程序获取Userdata

时间:2017-02-15 09:32:41

标签: c# asp.net-mvc asp.net-core-mvc asp.net-core-1.0 asp.net-core-identity

我正在构建Web应用程序,用户应该使用Google或Microsoft登录。 Authenticationprocess工作正常,但当我尝试获取有关用户的信息时,如电子邮件地址或First-secondmame,我总是得到一个Null-Object。 目前我使用基于Cookie的身份验证。

问题(S)

  • 我错了什么?
  • 如何解决此问题?

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

    ...
}

变量&#34; Infos&#34;的屏幕截图 Screenshot from the Variable "Infos"

修改 Startup.cs中的Cookieoptions:

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

1 个答案:

答案 0 :(得分:0)

我在UserManager中使用GetUserAsync方法来获取cur用户并且它对我有用,我希望你也是

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