Skip to content

Commit ada55c3

Browse files
committed
Finished the Logic part into the logics folder
1 parent 02afbd8 commit ada55c3

File tree

11 files changed

+3521
-140
lines changed

11 files changed

+3521
-140
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.AspNetCore.Mvc;
4+
using jwtauthcore.Models;
5+
using jwtauthcore.Interface;
6+
using Microsoft.AspNetCore.Authorization;
7+
8+
namespace jwtauthcore.Controllers
9+
{
10+
11+
[Authorize] // reference using Microsoft.AspNetCore.Authorization;
12+
[Route("api/controller")]
13+
[ApiController]
14+
public class AuthController : ControllerBase
15+
{
16+
private readonly IJwtAuthenticationManager jwtAuthenticationManager;
17+
18+
public AuthController(IJwtAuthenticationManager jwtAuthenticationManager)
19+
{
20+
this.jwtAuthenticationManager = jwtAuthenticationManager;
21+
}
22+
23+
24+
[HttpGet]
25+
public IEnumerable<string> Get()
26+
{
27+
return new string[] {"Bangladesh", "Dhaka"};
28+
}
29+
30+
[HttpGet("{id}", Name= "Get")]
31+
public string Get(int id)
32+
{
33+
return "value" + id;
34+
}
35+
36+
[AllowAnonymous] // everybody can call this mehtod but rest of the method is authorized.
37+
[HttpPost("authenticate")]
38+
public IActionResult Authenticate([FromBody] UserCred userCred)
39+
{
40+
var token = jwtAuthenticationManager.Authenticate(userCred.Username, userCred.Password);
41+
if(token == null)
42+
{
43+
return Unauthorized();
44+
}
45+
return Ok(token);
46+
}
47+
}
48+
}

jwtauthcore/Logics/JwtAuthenticationManager.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
using System;
12
using jwtauthcore.Interface;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.IdentityModel.Tokens.Jwt;
56
using System.Text;
7+
using Microsoft.IdentityModel.Tokens;
8+
using System.Security.Claims;
69

710
namespace jwtauthcore.Logics
811
{
@@ -29,8 +32,7 @@ public JwtAuthenticationManager(string key)
2932
this.key = key;
3033
}
3134

32-
33-
public Authentication(string username, string password)
35+
public string Authenticate(string username, string password)
3436
{
3537
// if there is no user and password matched from the dictionary
3638
// then I will return null
@@ -59,6 +61,23 @@ public Authentication(string username, string password)
5961
// thats why I am using this procedure.
6062
var tokenKey = Encoding.ASCII.GetBytes(key); // called the 'using System.Text;' for recognisiing the 'Encoding'
6163

64+
65+
// lets create the token descriptor
66+
// SecurityTokenDescriptor this class should load from this Library 'using Microsoft.IdentityModel.Tokens;'
67+
var tokenDescriptor = new SecurityTokenDescriptor
68+
{
69+
Subject = new ClaimsIdentity(new Claim[]
70+
{
71+
new Claim(ClaimTypes.Name, username)
72+
}), //ClaimsIdentity this class should load from 'using System.Security.Claims;'
73+
Expires = DateTime.UtcNow.AddHours(1), // our token will expire with in 1 hour
74+
SigningCredentials =
75+
new SigningCredentials(
76+
new SymmetricSecurityKey(tokenKey),
77+
SecurityAlgorithms.HmacSha256Signature) // called the sha Algorithm for making token
78+
};
79+
var token = tokenHandler.CreateToken(tokenDescriptor);
80+
return tokenHandler.WriteToken(token);
6281
}
6382
}
6483
}

jwtauthcore/Models/UserCred.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace jwtauthcore.Models
22
{
33
public class UserCred
44
{
5-
5+
public string Username {get;set;}
6+
7+
public string Password {get;set;}
68
}
79
}

jwtauthcore/Startup.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
using Microsoft.Extensions.DependencyInjection;
1111
using Microsoft.Extensions.Hosting;
1212
using Microsoft.Extensions.Logging;
13+
using jwtauthcore.Interface;
14+
using jwtauthcore.Logics;
15+
using Microsoft.AspNetCore.Authentication.JwtBearer;
16+
using Microsoft.IdentityModel.Tokens;
17+
using System.Text;
1318

1419
namespace jwtauthcore
1520
{
@@ -26,6 +31,27 @@ public Startup(IConfiguration configuration)
2631
public void ConfigureServices(IServiceCollection services)
2732
{
2833
services.AddControllers();
34+
35+
36+
var key = "this is test key";
37+
38+
services.AddAuthentication(x =>
39+
{
40+
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // here we need to install a nuget package 'using Microsoft.AspNetCore.Authentication.JwtBearer'.
41+
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; // package website link 'https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer/3.0.0'
42+
}).AddJwtBearer(x =>
43+
{
44+
x.RequireHttpsMetadata = false;
45+
x.SaveToken = true;
46+
x.TokenValidationParameters = new TokenValidationParameters // using Microsoft.IdentityModel.Tokens
47+
{
48+
ValidateIssuerSigningKey = true,
49+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(key)),
50+
ValidateIssuer = false,
51+
ValidateAudience = false
52+
};
53+
});
54+
services.AddSingleton<IJwtAuthenticationManager>(new JwtAuthenticationManager(key));
2955
}
3056

3157
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

jwtauthcore/jwtauthcore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
89
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.5.1" />
910
</ItemGroup>
1011

17.9 KB
Binary file not shown.
Binary file not shown.

jwtauthcore/obj/jwtauthcore.csproj.nuget.dgspec.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
"frameworks": {
4040
"netcoreapp3.1": {
4141
"dependencies": {
42+
"Microsoft.AspNetCore.Authentication.JwtBearer": {
43+
"target": "Package",
44+
"version": "[3.0.0, )"
45+
},
4246
"System.IdentityModel.Tokens.Jwt": {
4347
"target": "Package",
4448
"version": "[6.5.1, )"

jwtauthcore/obj/jwtauthcore.csproj.nuget.g.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
<PropertyGroup>
1313
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
1414
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
16+
<PkgNewtonsoft_Json Condition=" '$(PkgNewtonsoft_Json)' == '' ">C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\10.0.1</PkgNewtonsoft_Json>
17+
</PropertyGroup>
1518
</Project>

0 commit comments

Comments
 (0)