为什么signinmanager getexternallogininfoasync方法总是返回null?
我正在使用VS2015与MVC的默认asp.net核心(非框架)项目,个人用户帐户(这是一项要求)。使用第三方登录的目的是允许用户自行注册。基于角色的授权将使用通过Azure AD注册提供的身份由asp.net身份处理。
如果以下对经理登录的解释不正确,请更正我。此方法应提供有关外部登录的详细信息,并返回claimprincipal对象,该对象包含身份提供者提供的用户声明。
我使用以下指南在我的Startup.cs中设置OpenIdConnectAuthentication(下面的课程部分)
当我启动外部登录提供程序时,它会将我定向到组织登录页面并成功。
但是,应该由signinmanager方法填充的变量信息为空
如果我将一个断点放入回调类中,则填充User并且IsAuthenticated变量为true。
我可以驱动允许用户自己在应用程序中注册的功能,但是,这是我第一次尝试实施第三方登录,我想了解我在这一点上做错了。
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add Authentication services.
services.AddAuthentication(sharedOptions => {
sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
//services.AddDistributedMemoryCache();
//services.AddSession();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseIdentity();
// Configure the OWIN pipeline to use cookie auth.
app.UseCookieAuthentication( new CookieAuthenticationOptions());
//Add external authentication middleware below.To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme,
CallbackPath = "/signin-oidc",
ClientId = Configuration["AzureAD:ClientId"],
Authority = String.Format(Configuration["AzureAd:AadInstance"], Configuration["AzureAd:Tenant"]),
ResponseType = OpenIdConnectResponseType.IdToken,
PostLogoutRedirectUri = Configuration["AzureAd:PostLogoutRedirectUri"],
Events = new OpenIdConnectEvents
{
//OnRemoteFailure = OnAuthenticationFailed,
}
});
//app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
外部登录
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
// Request a redirect to the external login provider.
var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return Challenge(properties, provider);
}
答案 0 :(得分:3)
此方法已报告多个问题,过去产生空值。任何支持的开箱即用的身份验证方法都不会发生这种情况。至少在使用OAuth和azure AD并遵循帖子中提供的方法时,这是一个问题。但是,有一种解决方法仍然允许使用默认项目类型。只需将生成ExternalLoginInfo变量(info)的方法替换为使用用户原则构建的自己的ExternalLoginInfo类。
ExternalLoginInfo info = new ExternalLoginInfo(User,
"Microsoft",
User.Claims.Where(x=>x.Type== "http://schemas.microsoft.com/identity/claims/objectidentifier").FirstOrDefault().Value.ToString(),
"Microsoft" );
ASP.NET MVC 5 (VS2013 final): Facebook login with OWIN fails (loginInfo is null)
MVC 5 Owin Facebook Auth results in Null Reference Exception
答案 1 :(得分:1)
我在使用OpenIdConnect中间件时遇到了类似的问题-我终于通过将 ResponseType 更改为 OpenIdConnectResponseType.CodeIdToken (已设置为“代码”)来解决了这个问题。 / p>
这是我Startup.Auth.vb源的链接:
答案 2 :(得分:1)
删除 SignInScheme 后尝试
答案 3 :(得分:-4)
为什么你不使用内置的microsoftaccountauthentication。这里有一个教程。
这类似于谷歌和Facebook授权类。