Skip to content

Commit 02afbd8

Browse files
committed
Added the logic and Interface folder for structuring the project
1 parent c0fa756 commit 02afbd8

File tree

10 files changed

+382
-12
lines changed

10 files changed

+382
-12
lines changed

jwtauthcore/Controllers/NameController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace jwtauthcore.Controllers
88

99
[Route("api/controller")]
1010
[ApiController]
11-
public class NameController : ControllerBase
11+
public class AuthController : ControllerBase
1212
{
1313
[HttpGet]
1414
public IEnumerable<string> Get()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace jwtauthcore.Interface
2+
{
3+
public interface IJwtAuthenticationManager
4+
{
5+
string Authenticate(string username, string password);
6+
}
7+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using jwtauthcore.Interface;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.IdentityModel.Tokens.Jwt;
5+
using System.Text;
6+
7+
namespace jwtauthcore.Logics
8+
{
9+
// for fimilier with IJwtAuthenticationManager we need to call the Interface file here
10+
// thats why I used "using jwtauthcore.Interface;"
11+
public class JwtAuthenticationManager : IJwtAuthenticationManager
12+
{
13+
// this data will come from database, now I am using dictionary data so that
14+
// i do not need to implement database code right now
15+
16+
// for fimilier with IDictionary we need to call the Collection Library to this file
17+
// thats why I used "using System.Collections.Generic;"
18+
private readonly IDictionary<string, string> users = new Dictionary<string, string>
19+
{
20+
{"test1", "password1"},
21+
{"test2", "password2"}
22+
};
23+
24+
// Making a constructor for passing the key
25+
private readonly string key;
26+
27+
public JwtAuthenticationManager(string key)
28+
{
29+
this.key = key;
30+
}
31+
32+
33+
public Authentication(string username, string password)
34+
{
35+
// if there is no user and password matched from the dictionary
36+
// then I will return null
37+
38+
// for fimilier with Linq we need to call the LINQ Library to this file
39+
// thats why I used "using System.Linq;"
40+
if(!users.Any(u => u.Key == username && u.Value == password))
41+
{
42+
return null;
43+
}
44+
45+
// if matched with username and password
46+
// then I will pass the token to that user.
47+
48+
// here I used JwtSecurityTokenHandler class which will handle this token. This Class can be found on the 'nuget packages Library' which is made by Microsoft
49+
// website link 'https://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/'
50+
// As a result, we need to install the Library to this project because by default dotnet core framework dont implement it
51+
// so I need to install it from the terminal because I am doing this project from linux . Also I am using the VS code not the visual studio.
52+
53+
// From terminal we need to run this command ' dotnet add package System.IdentityModel.Tokens.Jwt --version 6.5.1'
54+
// After installing the Library we need to call that into this file
55+
// thats why I used 'using System.IdentityModel.Tokens.Jwt;'
56+
var tokenHandler = new JwtSecurityTokenHandler();
57+
58+
// As I mentioned earlier this token should be encrypted key
59+
// thats why I am using this procedure.
60+
var tokenKey = Encoding.ASCII.GetBytes(key); // called the 'using System.Text;' for recognisiing the 'Encoding'
61+
62+
}
63+
}
64+
}

jwtauthcore/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4040

4141
app.UseRouting();
4242

43+
app.UseAuthentication();
4344
app.UseAuthorization();
4445

4546
app.UseEndpoints(endpoints =>

jwtauthcore/jwtauthcore.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.5.1" />
9+
</ItemGroup>
10+
711

812
</Project>
3.27 KB
Binary file not shown.
Binary file not shown.

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"format": 1,
33
"restore": {
4-
"c:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj": {}
4+
"C:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj": {}
55
},
66
"projects": {
7-
"c:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj": {
7+
"C:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj": {
88
"version": "1.0.0",
99
"restore": {
10-
"projectUniqueName": "c:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj",
10+
"projectUniqueName": "C:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj",
1111
"projectName": "jwtauthcore",
12-
"projectPath": "c:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj",
12+
"projectPath": "C:\\Users\\Mohon\\Desktop\\jwtauthcore\\jwtauthcore.csproj",
1313
"packagesPath": "C:\\Users\\Mohon\\.nuget\\packages\\",
14-
"outputPath": "c:\\Users\\Mohon\\Desktop\\jwtauthcore\\obj\\",
14+
"outputPath": "C:\\Users\\Mohon\\Desktop\\jwtauthcore\\obj\\",
1515
"projectStyle": "PackageReference",
1616
"fallbackFolders": [
1717
"C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder"
@@ -38,6 +38,12 @@
3838
},
3939
"frameworks": {
4040
"netcoreapp3.1": {
41+
"dependencies": {
42+
"System.IdentityModel.Tokens.Jwt": {
43+
"target": "Package",
44+
"version": "[6.5.1, )"
45+
}
46+
},
4147
"imports": [
4248
"net461",
4349
"net462",

0 commit comments

Comments
 (0)