TL;DR - how to add authentication to an existing default core 2 web api project that was started without auth.
Details - I've got an existing .net core 2 web api project with no authentication configured and I'm using entity framework core.
It was opened like -
PIC 1 - No Auth Selected
I'd like to add Google authentication to my existing project as if it were opened with
PIC 2 - Individual user accounts selected
but I can't find any resource regarding adding those capabilities + scaffolding and migrations - all I can find are links regarding upgrading from core v1 to 2.
any ideas?
thanks!
Authentication schemes are specified by registering authentication services in Startup. ConfigureServices : By calling a scheme-specific extension method after a call to AddAuthentication (such as AddJwtBearer or AddCookie, for example). These extension methods use AuthenticationBuilder.
In IIS Manager, go to Features View, select Authentication, and enable Basic authentication. In your Web API project, add the [Authorize] attribute for any controller actions that need authentication. A client authenticates itself by setting the Authorization header in the request.
Add packages
Microsoft.AspNetCore.Identity
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.AspNetCore.Authentication.Google
Then in Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<IdentityUser, IdentityRole>();
services.AddAuthentication(
v => {
v.DefaultAuthenticateScheme = GoogleDefaults.AuthenticationScheme;
v.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
}).AddGoogle(googleOptions =>
{
googleOptions.ClientId = "CLIENT ID";
googleOptions.ClientSecret = "CLIENT SECRET";
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication()
.UseMvc();
}
A minimal working example here: https://github.com/mjrmua/Asp.net-Core-2.0-google-authentication-example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With