Skip to content

Commit

Permalink
User auth using open id connect
Browse files Browse the repository at this point in the history
  • Loading branch information
elanderson authored Jun 15, 2017
1 parent 6b97d28 commit 44f8c4b
Show file tree
Hide file tree
Showing 61 changed files with 20,677 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ClientApp/ClientApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<ItemGroup>
<PackageReference Include="IdentityModel" Version="2.9.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
Expand Down
8 changes: 8 additions & 0 deletions ClientApp/Controllers/IdentityController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ClientApp.Controllers
{
public class IdentityController : Controller
{
[Authorize]
public async Task<IActionResult> Index()
{
var discovery = await DiscoveryClient.GetAsync("http://localhost:5000");
Expand All @@ -24,5 +26,11 @@ public async Task<IActionResult> Index()

return View();
}

public async Task Logout()
{
await HttpContext.Authentication.SignOutAsync("Cookies");
await HttpContext.Authentication.SignOutAsync("oidc");
}
}
}
20 changes: 20 additions & 0 deletions ClientApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -35,6 +36,8 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

Expand All @@ -50,6 +53,23 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
app.UseExceptionHandler("/Home/Error");
}

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies"
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",

Authority = "http://localhost:5000",
RequireHttpsMetadata = false,

ClientId = "mvc",
SaveTokens = true
});

app.UseStaticFiles();

app.UseMvc(routes =>
Expand Down
17 changes: 16 additions & 1 deletion ClientApp/Views/Identity/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,19 @@
}

@ViewData["tokenResult"]
@ViewData["apiResult"]
@ViewData["apiResult"]

<h3>User claims</h3>

<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>

}
</dl>

<form asp-controller="Identity" asp-action="Logout" method="post">
<button type="submit">Logout</button>
</form>
60 changes: 60 additions & 0 deletions IdentityApp/Config.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using System.Security.Claims;
using IdentityServer4;
using IdentityServer4.Models;
using IdentityServer4.Test;

namespace IdentityApp
{
Expand Down Expand Up @@ -32,6 +35,63 @@ public static IEnumerable<Client> GetClients()

// scopes that client has access to
AllowedScopes = { "apiApp" }
},

// OpenID Connect implicit flow client (MVC)
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.Implicit,

RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
}
}
};
}

public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
};
}

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

Claims = new List<Claim>
{
new Claim("name", "Alice"),
new Claim("website", "https://alice.com")
}
},
new TestUser
{
SubjectId = "2",
Username = "bob",
Password = "password",

Claims = new List<Claim>
{
new Claim("name", "Bob"),
new Claim("website", "https://bob.com")
}
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions IdentityApp/IdentityApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="1.5.2" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
</ItemGroup>

</Project>
Loading

0 comments on commit 44f8c4b

Please sign in to comment.