Skip to content

Commit

Permalink
Updated api to use bear token security
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Mahan committed Apr 29, 2018
1 parent 86ffcca commit accaaab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
using System.Linq;
using CoreApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using IdentityServer4.AccessTokenValidation;

namespace CoreApi.Controllers {
[Route ("api/employee")]
[ApiController]

[Authorize (AuthenticationSchemes = IdentityServerAuthenticationDefaults.AuthenticationScheme)]
public class EmployeeController : Controller {
private readonly CoreApiContext _context;

Expand Down
17 changes: 17 additions & 0 deletions Controllers/IdentityController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using IdentityServer4.AccessTokenValidation;

[Route ("api/identity")]
[ApiController]

[Authorize (AuthenticationSchemes = IdentityServerAuthenticationDefaults.AuthenticationScheme)]
public class IdentityController : ControllerBase {
[HttpGet]
public IActionResult Get () {
return new JsonResult (User.Claims.Select (c => new { c.Type, c.Value }));
}
}
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static void Main(string[] args)
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://localhost:5002/")
.Build();
}
}
10 changes: 9 additions & 1 deletion Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ public Startup (IConfiguration configuration) {
public void ConfigureServices (IServiceCollection services) {

services.AddDbContext<CoreApiContext> (opt => opt.UseInMemoryDatabase ("Employee"));
services.AddMvc ();
services.AddMvcCore().AddApiExplorer().AddAuthorization().AddJsonFormatters();

services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options => {
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});

// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen (c => {
Expand All @@ -46,6 +53,7 @@ public void Configure (IApplicationBuilder app, IHostingEnvironment env) {
c.SwaggerEndpoint ("/swagger/v1/swagger.json", "My API V1");
});

app.UseAuthentication();
app.UseMvc ();
}
}
Expand Down
39 changes: 20 additions & 19 deletions core-api.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0-preview2-final" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.4.0" />
</ItemGroup>


</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.6.0" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0-preview2-final" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0-preview2-final" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.4.0" />
</ItemGroup>


</Project>

0 comments on commit accaaab

Please sign in to comment.