# 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/).
没有合适的资源?快使用搜索试试~ 我知道了~
AspNetSaml:适用于ASP.NETC#的非常简单的SAML 2.0使用者模块
共4个文件
cs:1个
license:1个
nuspec:1个
需积分: 50 19 下载量 37 浏览量
2021-04-29
20:10:07
上传
评论 1
收藏 10KB ZIP 举报
温馨提示
AspNetSaml C#中非常简单的SAML 2.0“消费者”实现。 这是一个SAML客户端库,而不是SAML服务器,它允许向您的ASP.NET应用程序添加SAML单点登录,但不能向其他应用程序提供身份验证服务。 安装 由一个简短的C#文件组成,您可以将其放入项目中(或)并开始使用它。 用法 SAML如何工作? SAML工作流程包括2个步骤: 用户被重定向到SAML提供程序(他在其中进行身份验证) 用户被重定向回您的应用,您可以在其中验证有效负载 这是您的操作方法(此示例适用于ASP.NET MVC: 1.将用户重定向到saml提供程序: // this example is an ASP.NET MVC action method public ActionResult Login () { // TODO: specify the SAML provider url h
资源推荐
资源详情
资源评论
收起资源包目录
AspNetSaml-master.zip (4个子文件)
AspNetSaml-master
Saml.cs 12KB
LICENSE 11KB
README.md 3KB
AspNetSaml.nuspec 890B
共 4 条
- 1
资源评论
EngleSEN
- 粉丝: 50
- 资源: 4502
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功