-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new packages - EMG.Extensions.AspNetCore - EMG.Extensions.AspNetCore.Authentication.JWT
- Loading branch information
Showing
30 changed files
with
1,406 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
samples/Authentication/JWT/Controllers/ValuesController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace JWT.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class ValuesController : ControllerBase | ||
{ | ||
// GET api/values | ||
[HttpGet] | ||
public ActionResult<IEnumerable<string>> Get() | ||
{ | ||
return new string[] { "value1", "value2" }; | ||
} | ||
|
||
// GET api/values/5 | ||
[HttpGet("{id}")] | ||
public ActionResult<string> Get(int id) | ||
{ | ||
return "value"; | ||
} | ||
|
||
// POST api/values | ||
[HttpPost] | ||
public void Post([FromBody] string value) | ||
{ | ||
} | ||
|
||
// PUT api/values/5 | ||
[HttpPut("{id}")] | ||
public void Put(int id, [FromBody] string value) | ||
{ | ||
} | ||
|
||
// DELETE api/values/5 | ||
[HttpDelete("{id}")] | ||
public void Delete(int id) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Authentication\JWT\JWT.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Update="appsettings.Development.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</Content> | ||
<Content Update="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JWT | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateWebHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory) | ||
.UseStartup<Startup>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using EMG.Extensions.AspNetCore; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace JWT | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); | ||
|
||
services.AddJwtAuthentication(Configuration) | ||
.RequireAuthentication() | ||
.AddBasicUserAuthenticator() | ||
.AddFormUserExtractor(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
app.UseHsts(); | ||
} | ||
|
||
|
||
app.UseJwtAuthentication(); | ||
|
||
|
||
app.UseHttpsRedirection(); | ||
app.UseMvc(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"JWT": { | ||
"SecretKey": "test-key-test-key-test-key", | ||
"IssuerOptions": { | ||
"Issuer": "Test API", | ||
"Audience": "https://localhost:5001" | ||
}, | ||
"Client": { | ||
"Username": "Test", | ||
"Password": "testing" | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Authentication/JWT/Authenticators/AlwaysTrueUserAuthenticator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace EMG.Extensions.AspNetCore.Authenticators | ||
{ | ||
public class AlwaysTrueUserAuthenticator : IUserAuthenticator | ||
{ | ||
public Task<bool> TryAuthenticateUserAsync(User user, out ClaimsIdentity identity) | ||
{ | ||
var claims = new[] { new Claim(ClaimTypes.Name, user.UserName) }; | ||
|
||
identity = new ClaimsIdentity(claims); | ||
|
||
return Task.FromResult(true); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Authentication/JWT/Authenticators/BasicUserAuthenticator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace EMG.Extensions.AspNetCore.Authenticators | ||
{ | ||
public class BasicUserAuthenticator : IUserAuthenticator | ||
{ | ||
private readonly BasicCredentials _pair; | ||
|
||
public BasicUserAuthenticator(BasicCredentials pair) | ||
{ | ||
_pair = pair ?? throw new ArgumentNullException(nameof(pair)); | ||
} | ||
|
||
public Task<bool> TryAuthenticateUserAsync(User user, out ClaimsIdentity identity) | ||
{ | ||
identity = null; | ||
|
||
if (user.UserName == _pair.Username && user.Password == _pair.Password) | ||
{ | ||
var claims = new[] { new Claim(ClaimTypes.Name, user.UserName) }; | ||
|
||
identity = new ClaimsIdentity(claims); | ||
|
||
return Task.FromResult(true); | ||
} | ||
|
||
return Task.FromResult(false); | ||
} | ||
} | ||
|
||
public class BasicCredentials | ||
{ | ||
[Required] | ||
public string Username { get; set; } | ||
|
||
[Required] | ||
public string Password { get; set; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Authentication/JWT/Authenticators/IUserAuthenticator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace EMG.Extensions.AspNetCore.Authenticators | ||
{ | ||
public interface IUserAuthenticator | ||
{ | ||
Task<bool> TryAuthenticateUserAsync(User user, out ClaimsIdentity identity); | ||
} | ||
} |
Oops, something went wrong.