@if (Context.User.Identity.Name != null)
{
<li class="text-white">
<a href="#">@Context.User.Identity.Name </a>
</li>
<li>
<form asp-controller="Account" asp-action="Logout" method="post">
<button type="submit" class="btn btn-link" name="provider" value="Saml2">Sign out</button>
</form>
</li>
}
else
{
<li>
<form asp-controller="Account" asp-action="ExternalLogin" method="post">
<button type="submit" class="btn btn-link" name="provider" value="Saml2">Sign In</button>
</form>
</li>
}
string encodedSaml = this.Request.Form["SAMLResponse"];
//EncodedeSAML = rawSamlData;
// the sample data sent us may be already encoded,
// which results in double encoding
if (encodedSaml.Contains('%'))
{
encodedSaml = System.Web.HttpUtility.UrlDecode(encodedSaml);
}
// read the base64 encoded bytes
string samlAssertion = Decode64Bit(encodedSaml);
DecodedSAML = samlAssertion;
//SamlParser(DecodedSAML);
//samldata = Decode64Bit("PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4=") + samldata;
string samldata = DecodedSAML;
if (!samldata.StartsWith(@"<?xml version="))
{
samldata = Decode64Bit("PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4=") + samldata;
}
string firstName = string.Empty;
XmlDocument xDoc = new XmlDocument();
samldata = samldata.Replace(@"\", "");
xDoc.LoadXml(samldata);
//xDoc.Load(new System.IO.TextReader());//Suppose the xml you have provided is stored in this xml file.
XmlNamespaceManager xMan = new XmlNamespaceManager(xDoc.NameTable);
xMan.AddNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
xMan.AddNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
xMan.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
XmlNode xNode = xDoc.SelectSingleNode("/samlp:Response/samlp:Status/samlp:StatusCode/@Value", xMan);
if (xNode != null)
{
this.AuthenticationStatus = false;
string statusCode = xNode.Value;
if (statusCode.EndsWith("status:Success"))
{
this.AuthenticationStatus = true;
}
}
// samlp:Response saml:Assertion saml:AttributeStatement saml:Attribute
xNode = xDoc.SelectSingleNode("/samlp:Response/saml:Assertion/saml:AttributeStatement/saml:Attribute[@Name = 'urn:oid:0.9.2342.19200300.100.1.1']/saml:AttributeValue", xMan);
if (xNode != null)
{
this.UserID = xNode.InnerText;
}
C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).
TRUSTED BY
ASKER
How do I get the username from the accountcontroller passed on this principal? Sorry I am not familiar with this concept so could you please let me know?