我正在尝试允许用户在我的ASP.NET Core Blazor应用程序上使用他们的谷歌帐户登录。每当我转到我的app/login/google-login时,一切都像预期的那样工作。我被重定向到谷歌的登录页面,我可以选择一个帐户进行登录。选择我的账号后,需要几秒钟的时间来加载,然后visual studio 2019会告诉我:
System.NullReferenceException: 'Object reference not set to an instance of an object.' Microsoft.AspNetCore.Authentication.AuthenticateResult.Principal.get returned null.
在这段代码中:
var claims = response.Principal.Identities.FirstOrDefault().Claims.Select(claim => new
{
claim.Issuer,
claim.OriginalIssuer,
claim.Type,
claim.Value
});一些调试显示:{"succeeded":false,"ticket":null,"principal":null,"properties":null,"failure":null,"none":true}:
这是我从格式化为JSON的Google API得到的响应。基本上,它告诉我主体为null,我可能已经猜到了,但其余的也是null。这里发生了什么事?这会不会仅仅是我在谷歌的范围内的一个问题?我有理由相信这不是问题所在,因为我的应用程序应该能够处理任何API响应而不会崩溃,对吧?
下面是我的LoginController.cs类:
[AllowAnonymous, Route("login")]
public class LoginController : Controller
{
[Route("google-login")]
public IActionResult GoogleLogin()
{
var properties = new AuthenticationProperties { RedirectUri = Url.Action("GoogleResponse") };
return Challenge(properties, GoogleDefaults.AuthenticationScheme);
}
[Route("google-response")]
public async Task<IActionResult> GoogleResponse()
{
var response = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
var claims = response.Principal.Identities.FirstOrDefault().Claims.Select(claim => new
{
claim.Issuer,
claim.OriginalIssuer,
claim.Type,
claim.Value
});
return Json(claims);
}
}这是我的Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
//services.AddSingleton<WeatherForecastService>();
services.AddDbContext<Context>(options => options.UseSqlServer(Configuration.GetConnectionString("Context")));
services.AddIdentity<User, Role>().AddEntityFrameworkStores<Context>();
services.AddHttpContextAccessor();
services.AddScoped<IReservationService, ReservationService>();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "/login/google-login";
})
.AddGoogle(options =>
{
options.ClientId = Configuration["Google:ClientID"];
options.ClientSecret = Configuration["Google:ClientSecret"];
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}如果您需要任何其他信息或代码,我很乐意尽快提供。
发布于 2021-05-30 20:32:34
这条线,可以说是我项目中相当大的一部分,因为谁知道是什么原因而阻止了与谷歌的交流。
services.AddIdentity<User, Role>().AddEntityFrameworkStores<Context>();我现在基本上没有使用UserManager和RoleManager,只是手动编写了访问AspNetUsers等的方法。
可能不是一个真正的解决方案,但它就是这样的。
https://stackoverflow.com/questions/67690374
复制相似问题