Displayr.AspNetSaml 1.1.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Displayr.AspNetSaml --version 1.1.6
NuGet\Install-Package Displayr.AspNetSaml -Version 1.1.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Displayr.AspNetSaml" Version="1.1.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Displayr.AspNetSaml --version 1.1.6
#r "nuget: Displayr.AspNetSaml, 1.1.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Displayr.AspNetSaml as a Cake Addin
#addin nuget:?package=Displayr.AspNetSaml&version=1.1.6

// Install Displayr.AspNetSaml as a Cake Tool
#tool nuget:?package=Displayr.AspNetSaml&version=1.1.6

AspNetSaml

Build status

Very simple SAML 2.0 "consumer" implementation in C#. It's a SAML client library, not a SAML server, allows adding SAML single-sign-on to your ASP.NET app, but not to provide auth services to other apps.

It's a SAML client library, not a SAML server, allows adding SAML single-sign-on to your ASP.NET app, but not to provide auth services to other apps.

Installation

Consists of one short C# file you can throw into your project (or install via nuget) and start using it.

Usage

How SAML works?

SAML workflow has 2 steps:

  1. User is redirected to the SAML provider (where he authenticates)
  2. User is redirected back to your app, where you validate the payload

Here's how you do it (this example is for ASP.NET MVC:

1. Redirecting the user to the saml provider:

//this example is an ASP.NET MVC action method
public ActionResult Login()
{
	//TODO: specify the SAML provider url here, aka "Endpoint"
	var samlEndpoint = "http://saml-provider-that-we-use.com/login/";

	var request = new AuthRequest(
		"http://www.myapp.com", //TODO: put your app's "entity ID" here
		"http://www.myapp.com/SamlConsume" //TODO: put Assertion Consumer URL (where the provider should redirect users after authenticating)
		);

	//redirect the user to the SAML provider
	return Redirect(request.GetRedirectUrl(samlEndpoint));
}

2. User has been redirected back

User is sent back to your app - you need to validate the SAML response ("assertion") that you recieved via POST.

Here's an example of how you do it in ASP.NET MVC

//ASP.NET MVC action method... But you can easily modify the code for Web-forms etc.
public ActionResult SamlConsume()
{
	// 1. TODO: specify the certificate that your SAML provider gave you
	string samlCertificate = @"-----BEGIN CERTIFICATE-----
BLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAH123543==
-----END CERTIFICATE-----";

	// 2. Let's read the data - SAML providers usually POST it into the "SAMLResponse" var
	Saml.Response samlResponse = new Response(samlCertificate, Request.Form["SAMLResponse"]);

	// 3. We're done!
	if (samlResponse.IsValid())
		username = samlResponse.GetNameID();
}

Reading more attributes from the provider

SAML providers usually send more data with their response: username, first/last names etc. Here's how to get it:

if (samlResponse.IsValid())
{
	//WOOHOO!!! user is logged in

	//Some more optional stuff for you
	//let's extract username/firstname etc
	string username, email, firstname, lastname;
	try
	{
		username = samlResponse.GetNameID();
		email = samlResponse.GetEmail();
		firstname = samlResponse.GetFirstName();
		lastname = samlResponse.GetLastName();
	}
	catch(Exception ex)
	{
		//insert error handling code
		//no, really, please do
		return null;
	}

	//user has been authenticated, put your code here, like set a cookie or something...
	//or call FormsAuthentication.SetAuthCookie() or something
}

Dependencies

Depending on your .NET version, your Project should reference System.Security for .NET Framework and System.Security.Cryptography.Xml for .NET Core.

Testing

Tests are Visual Studio tests. Build the solution and you should find them in the Test Explorer. These rely on:

  • The test Azure Active Directory called "Saml Single Signon Test Organisation"
  • In which the "Displayr (saml-test-master)" Enterprise Application is registered, including a reference to the redirection URL (below).
  • An Azure function that serves the redirection URL. (Constants.cs:REPLY_URL)

Nuget

I've published this to Nuget.

Install-Package Displayr.AspNetSaml

This will add a reference to a compiled version of this assembly.

Nuget Publishing Steps

  1. Open the solution in Visual Studio
  2. Do a release build
  3. Copy the build output Saml.dll to the lib folder.
  4. Update Saml.Nuspec version number, and please use the SEMVER (https://semver.org/) scheme to decide which digit to increment. Commit this and push.
  5. Update Saml.Nuspec releasenotes fields and put some description of what you changed there. But don't commit this.
  6. From the Package Manage Console, run this command:

nuget pack Saml\Saml.nuspec

  1. Ignore warnings about using bin folders.
  2. This will generate a file like Displayr.AspNetSaml.1.1.0.nupkg in your solution root folder.
  3. Visit https://www.nuget.org/packages/Displayr.AspNetSaml/ and login, and click upload.
  4. Upload the nupkg file generated.
  5. In the preview, enter https://raw.githubusercontent.com/Displayr/AspNetSaml/master/README.md for the doco url.
  6. Click Submit

About Displayr

Displayr

Powerful business intelligence and online reporting for survey data. Now hiring...

Company: https://www.displayr.com
Careers: https://www.displayr.com/careers/

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.8 17,057 7/12/2023
1.1.7 695 3/21/2023
1.1.6 7,152 3/8/2021
1.1.5 2,135 1/29/2021
1.1.4 403 1/20/2021
1.1.3 1,791 12/5/2019
1.1.2 489 12/5/2019
1.1.1 527 12/5/2019
1.1.0 520 12/5/2019
1.0.3 826 5/1/2019
1.0.2 921 4/3/2019
1.0.1 543 4/3/2019

Add FirstName and LastName to the list of attribute names to check for first and last name respectively.