Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net core identity + api

I just started learning asp.net core. I would like to create a simple web app, where I would have a rest API in asp.net core and then a separate frontend with some angular consuming that API.

I just got a bit stuck trying to figure out ASP.NET Core Identity and cookie/token authentication...

My question is pretty simple: Can I just create an API and use Entity Framework for database handling and ASP.NET Core Identity to handle creating and managing users and authorization? Do I have to also to use some JWT, OAuth or anything like that? It's just this is all super new to me and I am getting confused, because every example/tutorial shows it in a different way and I am getting very confused...

Thanks for any help!

like image 336
marc08 Avatar asked Jan 11 '17 11:01

marc08


People also ask

What is ASP.NET identity in Web API?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.

Does ASP.NET Core have Web API?

ASP.NET Core supports creating web APIs using controllers or using minimal APIs. Controllers in a web API are classes that derive from ControllerBase.


1 Answers

I'm working on a project that's very similar. Check out IdentityServer4 https://identityserver4.readthedocs.io/en/release/index.html. It's an open source OpenID Connect/OAuth 2 framework for ASP.NET Core created by the guys from leastprivilege https://leastprivilege.com.

You can handle protecting your APIs with JWTs and configure IdentityServer to use ASP.NET Core Identity for its user store. This section here describes protecting the API: https://identityserver4.readthedocs.io/en/release/configuration/apis.html

This is basically how you add ASP.NET Identity, IdentityServer, and configure IdentityServer to use ASP.NET Identity in your Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

services.AddMvc();

// Adds IdentityServer
services.AddIdentityServer()
    .AddAspNetIdentity<ApplicationUser>();
}

Then protecting an API is just a few lines of code in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
    {
        Authority = "https://demo.identityserver.io",
        AllowedScopes = { "api1" },
    });

    app.UseMvc();
}

Then you would have to configure your angular app to be a "client" of IdentityServer and be able to access your API "resource". There is a whole tutorial on adding JavaScript clients: https://identityserver4.readthedocs.io/en/release/quickstarts/7_javascript_client.html

like image 89
dpjas Avatar answered Sep 30 '22 14:09

dpjas