# AspNetSaml
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.
## Installation
Consists of **one short C# file** you can throw into your project (or [install via nuget](#new-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)
1. 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:
```c#
//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
```c#
//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:
```c#
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.
# (NEW!) Nuget
I've published this to Nuget.
`Install-Package AspNetSaml`
This will simply add the cs-file to the root of your project.
A version of this library has been used for years in production in our [helpdesk app](https://jitbit.github.com/helpdesk/).
EngleSEN
- 粉丝: 54
- 资源: 4502
最新资源
- 嵌入式第二课 GPIO口的认识与使用
- 焊丝送丝设备sw18全套技术资料100%好用.zip
- 计算机网络校园网课程设计
- Cisco-300-410.pdf
- 回旋提升式柔性链输送机sw16可编辑全套技术资料100%好用.zip
- 机加工磨床sw16可编辑全套技术资料100%好用.zip
- website-fgmalatest.zip
- 奖牌徽章边角自动打磨机_x_t全套技术资料100%好用.zip
- 鸡蛋自动分配机sw19全套技术资料100%好用.zip
- 激光打标+视觉贴标+视觉装夹扣生产线x_t全套技术资料100%好用.zip
- 精密电子切割机sw17全套技术资料100%好用.zip
- 武汉理工大学通信原理课程设计(2ASK抗噪声性能分析matlab代码)
- 计算机网络校园网课程设计
- 中国空间站的发展历史学习空间站知识宣传介绍PPT.pptx
- 计算机网络校园网课程设计
- 面向一年级的航天知识及我国航天发展科普
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈