/// <summary> /// Performs active authentication against ADFS using the trust/13/usernamemixed ADFS endpoint. /// </summary> /// <param name="siteUrl">Url of the SharePoint site that's secured via ADFS</param> /// <param name="serialNumber">Serial Number of the Current User > My Certificate to use to authenticate </param> /// <param name="certificateMixed">Uri to the ADFS certificatemixed endpoint</param> /// <param name="relyingPartyIdentifier">Identifier of the ADFS relying party that we're hitting</param> /// <param name="logonTokenCacheExpirationWindow">Logon TokenCache expiration window integer value</param> /// <returns>A cookiecontainer holding the FedAuth cookie</returns> public CookieContainer GetFedAuthCookie(string siteUrl, string serialNumber, Uri certificateMixed, string relyingPartyIdentifier, int logonTokenCacheExpirationWindow) { CertificateMixed adfsTokenProvider = new CertificateMixed(); var token = adfsTokenProvider.RequestToken(serialNumber, certificateMixed, relyingPartyIdentifier); string fedAuthValue = TransformSamlTokenToFedAuth(token.TokenXml.OuterXml, siteUrl, relyingPartyIdentifier); // Construct the cookie expiration date TimeSpan lifeTime = SamlTokenlifeTime(token.TokenXml.OuterXml); if (lifeTime == TimeSpan.Zero) { lifeTime = new TimeSpan(0, 60, 0); } int cookieLifeTime = Math.Min((lifeTime.Hours * 60 + lifeTime.Minutes), logonTokenCacheExpirationWindow); DateTime expiresOn = DateTime.Now.AddMinutes(cookieLifeTime); CookieContainer cc = null; if (!string.IsNullOrEmpty(fedAuthValue)) { cc = new CookieContainer(); Cookie samlAuth = new Cookie("FedAuth", fedAuthValue); samlAuth.Expires = expiresOn; samlAuth.Path = "/"; samlAuth.Secure = true; samlAuth.HttpOnly = true; Uri samlUri = new Uri(siteUrl); samlAuth.Domain = samlUri.Host; cc.Add(samlAuth); } return(cc); }
/// <summary> /// Performs active authentication against ADFS using the trust/13/usernamemixed ADFS endpoint. /// </summary> /// <param name="siteUrl">Url of the SharePoint site that's secured via ADFS</param> /// <param name="serialNumber">Serial Number of the Current User > My Certificate to use to authenticate </param> /// <param name="certificateMixed">Uri to the ADFS certificatemixed endpoint</param> /// <param name="relyingPartyIdentifier">Identifier of the ADFS relying party that we're hitting</param> /// <param name="logonTokenCacheExpirationWindow"></param> /// <returns>A cookiecontainer holding the FedAuth cookie</returns> public CookieContainer GetFedAuthCookie(string siteUrl, string serialNumber, Uri certificateMixed, string relyingPartyIdentifier, int logonTokenCacheExpirationWindow) { CertificateMixed adfsTokenProvider = new CertificateMixed(); var token = adfsTokenProvider.RequestToken(serialNumber, certificateMixed, relyingPartyIdentifier); string fedAuthValue = TransformSamlTokenToFedAuth(token.TokenXml.OuterXml, siteUrl, relyingPartyIdentifier); // Construct the cookie expiration date TimeSpan lifeTime = SamlTokenlifeTime(token.TokenXml.OuterXml); if (lifeTime == TimeSpan.Zero) { lifeTime = new TimeSpan(0, 60, 0); } int cookieLifeTime = Math.Min((lifeTime.Hours * 60 + lifeTime.Minutes), logonTokenCacheExpirationWindow); DateTime expiresOn = DateTime.Now.AddMinutes(cookieLifeTime); CookieContainer cc = null; if (!string.IsNullOrEmpty(fedAuthValue)) { cc = new CookieContainer(); Cookie samlAuth = new Cookie("FedAuth", fedAuthValue); samlAuth.Expires = expiresOn; samlAuth.Path = "/"; samlAuth.Secure = true; samlAuth.HttpOnly = true; Uri samlUri = new Uri(siteUrl); samlAuth.Domain = samlUri.Host; cc.Add(samlAuth); } return cc; }