'MVC Web API NET CORE 5.0 Google Auth: No webpage was found for the web address: https://localhost:44307/signin-google
Just built a brand new web api, followed this example: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-5.0 as best I could understand. Not using razor pages or a database. And had to add the following because it wouldn't run otherwise:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = GoogleDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
Created the auth token in https://console.cloud.google.com/apis/credentials, stored them locally, and set the redirect to https://localhost:44307/signin-google as suggested.
This is my complete code.
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
namespace WebApplication3
{
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.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = GoogleDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
}).AddGoogle(options =>
{
IConfigurationSection googleAuthNSection =
Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
});
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication3", Version = "v1" });
});
}
// 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();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication3 v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseStaticFiles();
app.UseAuthorization();
app.UseMvc();
}
}
}
And the controller:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
namespace WebApplication3.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
[HttpGet]
[Authorize]
public string Get()
{
return User.FindFirst(ClaimTypes.Email)?.Value;
}
}
}
And this is the launchsettings.json
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:6108",
"sslPort": 44307
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApplication3": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
After authenticating with Google, I get this 404 error. The Middleware that handles the redirect is not working. What am I missing?
This localhost page can’t be found No webpage was found for the web address: https://localhost:44307/signin-google?state=CfDJ8AQzN35V4MFOkj0na0CgYAVyRcb43o1mYJ8O0-0OFiTiRbXtG4ACLk4IOEVRUP6cQ1B4Goxb6GWtDzDHKc6tsl-Hibm9CC9IyJCALL2Xj18HlmGNv6wmSUiQw8Dh8hJB6pPembsPHBdUmNFREHdU2uIQzGbUYsCIfH6kgNpqTysF0mWNov5BkxaPkzyknqUrmcrOt1_crcVPYzprhnjyxiPutE4Wk4EpvNE-TYeWP2V9&code=4%2F0AX4XfWjWkwE4_Uj-es9mmq29QVaWpcLbwhJ95r0CpNlfvLKKedTVnWZd1qlkIl8nCDvSOg&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=0&prompt=consent# HTTP ERROR 404
Solution 1:[1]
It's missing in Startup.cs:
app.UseAuthentication();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | O. C. |
