A day with .Net

My day to day experince in .net

Micro Services Architecture – Design Authentication with IdentityServer4, SQL Server and ASP.NET Core Part-1

Posted by vivekcek on January 20, 2018

Micro Services architecture is one of the hot topic in developer community.

I recommend micro services architecture if you face the below scenarios.

1. Use it when you are going to build next Amazon, Facebook, Uber etc.
2. You need to build a large system with a set of people with different technology stack.
3. You are building for high availability and fault tolerance.
4. You want to reduce the time to market.

To succeed with this architecture. You need below thing

1. A team of good architects who understand the architecture and business domain very well.
2. Everyday refine the architecture, if you find any flaws.
3. Should be able to define the boundaries of each micro services.
4. First day onward design the architecture for availability, Security, resilience.
5. Agile is good but execute it with creative people, who know how to execute it better.

In micro services architecture the first thing we can do is design our authentication system.

Here I am going to setup a Token Based authentication system with Identity Server 4. This is how it looks.

These are my Tools.

1.Visual Studio 2017(15.0)
2.ASP.NET Core 1.1
3.SQL Server 2014
4.Post Man

Please note that the steps will be different for ASP.NET Core 2.0.

1.Create an ASP.NET Core 1.1 project.

2.Select empty template.

3.Now add the IdentityServer 4 nuget package (We are using 1.5.2 version , 1.x versions are for ASP.NET Core 1.1).

4.We want our users to be signed with Username and Password.
5.Now add a class named Config.cs in your solution and paste below code.

using IdentityServer4.Models;
using IdentityServer4.Test;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IdentityServices
{
    public class Config
    {

        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource("api1", "My API")
            };
        }

        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
              
                new Client
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = { "api1" }
                }
            };
        }


        public static List<TestUser> GetUsers()
        {
            return new List<TestUser>
            {
                new TestUser
                {
                    SubjectId = "1",
                    Username = "vivek",
                    Password = "password"
                }
               
            };
        }

    }

   
}

6.Now update your Startup.cs as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace IdentityServices
{
    public class Startup
    {
        
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddInMemoryApiResources(Config.GetApiResources())
                .AddInMemoryClients(Config.GetClients())
                .AddTestUsers(Config.GetUsers());
        }

       
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {

            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();

        }
    }
}

7.Now open your project properties and in Debug tab change profile from IISExpress to your project, then update you app url to http://localhost:5000/

8.Now run the app. Select IdentityServices.

9.The app will run as a console application.

10.Now open post man and try this.

In the next post we will move the users to SQL Server.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s