Created
August 13, 2018 02:18
-
-
Save componentspace/995ff9eee9bbdd30a4e0e73dc1a236d7 to your computer and use it in GitHub Desktop.
IdentityServer4 as a SAML IdP using ComponentSpace - Startup class
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
using IdentityServer4; | |
using IdentityServer4.Models; | |
using IdentityServer4.Services; | |
using IdentityServer4.Stores; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using System; | |
namespace TestIdentityServer4 | |
{ | |
public class Startup | |
{ | |
private readonly IConfiguration _config; | |
public Startup(IConfiguration config) | |
{ | |
_config = config; | |
} | |
// This method gets called by the runtime. Use this method to add services to the container. | |
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddIdentityServer() | |
.AddDeveloperSigningCredential() | |
.AddInMemoryIdentityResources(Config.GetIdentityResources()) | |
.AddInMemoryApiResources(Config.GetApiResources()) | |
.AddInMemoryClients(Config.GetClients()) | |
.AddTestUsers(Config.GetUsers()); | |
// Add SAML SSO services. | |
services.AddSaml(_config.GetSection("SAML")); | |
// Add SAML middleware services. | |
services.AddSamlMiddleware(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
app.UseIdentityServer(); | |
// Use SAML middleware. | |
app.UseSaml(); | |
// Specify the display name and return URL for logout. | |
app.Use(async (context, next) => | |
{ | |
if (context.Request.Path.Value.Equals("/Account/Logout", StringComparison.OrdinalIgnoreCase) && | |
string.IsNullOrEmpty(context.Request.Query["logoutId"])) | |
{ | |
var identityServerInteractionService = context.RequestServices.GetRequiredService<IIdentityServerInteractionService>(); | |
var logoutMessageStore = context.RequestServices.GetRequiredService<IMessageStore<LogoutMessage>>(); | |
var logoutMessage = new Message<LogoutMessage>(new LogoutMessage | |
{ | |
ClientName = "SAML Service Provider", | |
PostLogoutRedirectUri = "/SAML/SingleLogoutServiceCompletion" | |
}, | |
DateTime.UtcNow); | |
var logoutId = await logoutMessageStore.WriteAsync(logoutMessage); | |
context.Request.QueryString = context.Request.QueryString.Add("logoutId", logoutId); | |
} | |
await next(); | |
}); | |
app.UseStaticFiles(); | |
app.UseMvcWithDefaultRoute(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment