Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IdentityServer4 as a SAML IdP using ComponentSpace - Startup class
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
You can’t perform that action at this time.