alanmacgowan / IdentityServerSample

Sample ASP.NET Core MVC and Angular apps and API using Identity Server 4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IdentityServerSample

Sample ASP.NET Core MVC app, Angular and API using Identity Server 4 and Entity Framework to store Identity tables (Users, Roles, UserRoles, UserClaims, UserLogins, etc) and Identity Server tables (Clients, ClientSecrets, PersistedGrants, IdentityResources, ApiResources, etc)

Quick start

Edit soution properties to set multiple startup projects (select API, WebApp, AngularApp and IdentityServer as start projects).
Then just run F5, this will start the Identity Server on https://localhost:44367/, API on https://localhost:44374/, Angular App on https://localhost:44398/ and Web App on https://localhost:44340/.
First run will initialize the database IdentityServerSample with all the tables and some sample data and Identity Server configuration data.
Go to Web App -> SignIn, this will redirect to Identity Server. Register if it's first time, accept consent and once logged in can access API resources.

Diagram

Scenarios Covered

  • User Authentication (WebApp)
  • User Authentication (AngularApp)
  • User Authentication from javascript (oidc-client.js)
  • User Authentication with 3rd party provider (Google)
  • Call API from C# (WebApp)
  • Call API from javascript (WebApp)
  • Call API from Angular (AngularApp)
  • Long lived API access using refresh tokens (allow requesting new access tokens without user interaction)

Steps

1 - IdentityServer config:

(Config.cs)
    a. Clients:
        i.  MVC application (HybridClientCredentials) ["mvc"]
        ii. SPA application (Implicit) ["Spa"]
    b. Resources:
        i.  Identity (OpenId, Profile)
        ii. Api ["api1"]

(Startup.cs)
    a. AddIdentity
    b. AddIdentityServer
          AddDeveloperSigningCredential
          AddAspNetIdentity
          AddConfigurationStore
          AddOperationalStore
    c. AddAuthentication
          AddGoogle
    
2 - MVC application config:

(Startup.cs)
    a. AddAuthentication
          AddCookie
          AddOpenIdConnect
                          
3 - API config:

(Startup.cs)
    a. AddAuthentication
          AddJwtBearer
    b. AddAuthorization
    c. AddCors     
          

Useful Resources

Identity Server

JWT

Pluralsight Courses

About

Sample ASP.NET Core MVC and Angular apps and API using Identity Server 4

License:MIT License


Languages

Language:C# 62.5%Language:CSS 28.3%Language:JavaScript 4.4%Language:TypeScript 3.5%Language:HTML 1.3%