forked from i8beef/SAML2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaml20MetadataHandler.cs
More file actions
93 lines (79 loc) · 3.73 KB
/
Saml20MetadataHandler.cs
File metadata and controls
93 lines (79 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Web;
using SAML2.Config;
namespace SAML2.Protocol
{
/// <summary>
/// The handler that exposes a metadata endpoint to the other parties of the federation.
/// The handler accepts the following GET parameters :
/// - encoding : Delivers the Metadata document in the specified encoding. Example: encoding=iso-8859-1 . If the parameter is omitted, the encoding utf-8 is used.
/// - sign : A boolean parameter specifying whether to sign the metadata document. Example: sign=false. If the parameter is omitted, the document is signed.
/// </summary>
public class Saml20MetadataHandler : AbstractEndpointHandler
{
#region IHttpHandler Members
/// <summary>
/// Gets a value indicating whether this instance is reusable.
/// </summary>
/// <value>
/// <c>true</c> if this instance is reusable; otherwise, <c>false</c>.
/// </value>
public new bool IsReusable
{
get { return false; }
}
/// <summary>
/// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
public override void ProcessRequest(HttpContext context)
{
var encoding = context.Request.QueryString["encoding"];
try
{
if (!string.IsNullOrEmpty(encoding))
{
context.Response.ContentEncoding = Encoding.GetEncoding(encoding);
}
}
catch (ArgumentException ex)
{
Logger.ErrorFormat(ErrorMessages.UnknownEncoding, encoding);
throw new ArgumentException(string.Format(ErrorMessages.UnknownEncoding, encoding), ex);
}
var sign = true;
var param = context.Request.QueryString["sign"];
if (!string.IsNullOrEmpty(param))
{
if (!bool.TryParse(param, out sign))
{
throw new ArgumentException(ErrorMessages.MetadataSignQueryParameterInvalid);
}
}
context.Response.ContentType = Saml20Constants.MetadataMimetype;
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"metadata.xml\"");
CreateMetadataDocument(context, sign);
context.Response.End();
}
#endregion
/// <summary>
/// Creates the metadata document.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="sign">if set to <c>true</c> sign the document.</param>
private void CreateMetadataDocument(HttpContext context, bool sign)
{
Logger.Debug(TraceMessages.MetadataDocumentBeingCreated);
var configuration = Saml2Config.Current;
var keyinfo = new KeyInfo();
var keyClause = new KeyInfoX509Data(configuration.ServiceProvider.SigningCertificate.GetCertificate(), X509IncludeOption.EndCertOnly);
keyinfo.AddClause(keyClause);
var doc = new Saml20MetadataDocument(configuration, keyinfo, sign);
Logger.Debug(TraceMessages.MetadataDocumentCreated);
context.Response.Write(doc.ToXml(context.Response.ContentEncoding));
}
}
}