Skip to content

Commit 713f7e1

Browse files
Minor performance improvements (#93)
* Minor performance improvements Made some improvements that improves performance a small amount, but cuts allocations by more than half. Also changed the accessors and added `readonly` to help enforce design guidelines. * Update Saml.cs * Reverted URL encoding method
1 parent d372c98 commit 713f7e1

File tree

1 file changed

+29
-34
lines changed

1 file changed

+29
-34
lines changed

AspNetSaml/Saml.cs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Use this freely under the Apache license (see https://choosealicense.com/license
1313
using System.Security.Cryptography.Xml;
1414
using System.IO.Compression;
1515
using System.Text;
16-
using System.Runtime;
1716

1817
namespace Saml
1918
{
@@ -283,35 +282,35 @@ protected override bool IsExpired()
283282

284283
public abstract class BaseRequest
285284
{
286-
public string _id;
287-
protected string _issue_instant;
288-
289-
protected string _issuer;
290-
291-
public BaseRequest(string issuer)
292-
{
293-
_id = "_" + Guid.NewGuid().ToString();
285+
protected readonly string _id;
286+
protected readonly string _issue_instant;
287+
protected readonly string _issuer;
288+
289+
protected static readonly XmlWriterSettings _xmlSettings = new XmlWriterSettings {
290+
OmitXmlDeclaration = true,
291+
Encoding = new UTF8Encoding(false)
292+
};
293+
294+
protected BaseRequest(string issuer) {
295+
_id = $"_{Guid.NewGuid()}";
294296
_issue_instant = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture);
295297

296298
_issuer = issuer;
297299
}
298300

299301
public abstract string GetRequest();
300302

301-
protected static string ConvertToBase64Deflated(string input)
302-
{
303-
//byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(input);
304-
//return System.Convert.ToBase64String(toEncodeAsBytes);
303+
protected static string ConvertToBase64Deflated(MemoryStream streamInput) {
304+
streamInput.Seek(0, SeekOrigin.Begin);
305305

306306
//https://stackoverflow.com/questions/25120025/acs75005-the-request-is-not-a-valid-saml2-protocol-message-is-showing-always%3C/a%3E
307-
var memoryStream = new MemoryStream();
308-
using (var writer = new StreamWriter(new DeflateStream(memoryStream, CompressionMode.Compress, true), new UTF8Encoding(false)))
309-
{
310-
writer.Write(input);
311-
writer.Close();
307+
using (var compressed = new MemoryStream()) {
308+
using (var deflate = new DeflateStream(compressed, CompressionMode.Compress, leaveOpen: true)) {
309+
streamInput.CopyTo(deflate);
310+
}
311+
312+
return Convert.ToBase64String(compressed.GetBuffer(), 0, (int)compressed.Length, Base64FormattingOptions.None);
312313
}
313-
string result = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Base64FormattingOptions.None);
314-
return result;
315314
}
316315

317316
/// <summary>
@@ -322,7 +321,7 @@ protected static string ConvertToBase64Deflated(string input)
322321
/// <returns></returns>
323322
public string GetRedirectUrl(string samlEndpoint, string relayState = null)
324323
{
325-
var queryStringSeparator = samlEndpoint.Contains("?") ? "&" : "?";
324+
var queryStringSeparator = samlEndpoint.Contains('?') ? '&' : '?';
326325

327326
var url = samlEndpoint + queryStringSeparator + "SAMLRequest=" + Uri.EscapeDataString(GetRequest());
328327

@@ -337,7 +336,7 @@ public string GetRedirectUrl(string samlEndpoint, string relayState = null)
337336

338337
public class AuthRequest : BaseRequest
339338
{
340-
private string _assertionConsumerServiceUrl;
339+
private readonly string _assertionConsumerServiceUrl;
341340

342341
/// <summary>
343342
/// Initializes new instance of AuthRequest
@@ -369,11 +368,9 @@ public enum AuthRequestFormat
369368
/// <returns></returns>
370369
public override string GetRequest()
371370
{
372-
using (StringWriter sw = new StringWriter())
371+
using (var ms = new MemoryStream())
373372
{
374-
XmlWriterSettings xws = new XmlWriterSettings { OmitXmlDeclaration = true };
375-
376-
using (XmlWriter xw = XmlWriter.Create(sw, xws))
373+
using (var xw = XmlWriter.Create(ms, _xmlSettings))
377374
{
378375
xw.WriteStartElement("samlp", "AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
379376
xw.WriteAttributeString("ID", _id);
@@ -403,7 +400,7 @@ public override string GetRequest()
403400
xw.WriteEndElement();
404401
}
405402

406-
return ConvertToBase64Deflated(sw.ToString());
403+
return ConvertToBase64Deflated(ms);
407404
}
408405
}
409406
}
@@ -413,20 +410,18 @@ public override string GetRequest()
413410
/// </summary>
414411
public class SignoutRequest : BaseRequest
415412
{
416-
private string _nameId;
413+
private readonly string _nameId;
417414

418415
public SignoutRequest(string issuer, string nameId) : base(issuer)
419416
{
420417
_nameId = nameId;
421418
}
422-
419+
423420
public override string GetRequest()
424421
{
425-
using (StringWriter sw = new StringWriter())
422+
using (var ms = new MemoryStream())
426423
{
427-
XmlWriterSettings xws = new XmlWriterSettings { OmitXmlDeclaration = true };
428-
429-
using (XmlWriter xw = XmlWriter.Create(sw, xws))
424+
using (var xw = XmlWriter.Create(ms, _xmlSettings))
430425
{
431426
xw.WriteStartElement("samlp", "LogoutRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
432427
xw.WriteAttributeString("ID", _id);
@@ -444,7 +439,7 @@ public override string GetRequest()
444439
xw.WriteEndElement();
445440
}
446441

447-
return ConvertToBase64Deflated(sw.ToString());
442+
return ConvertToBase64Deflated(ms);
448443
}
449444
}
450445
}

0 commit comments

Comments
 (0)