troubleshooting Question

Context.User.Identity.name in C#

Avatar of angel7170
angel7170Flag for United States of America asked on
C#
3 Comments1 Solution41 ViewsLast Modified:
Hello Experts,

I have successfully parsed SAML response and got the username from the SAML assertion attribute (this.UserID holds the username)  but I don't know how to pass this username to Context.User.Identity.name in the startup.cs. Could you please please help? Any assistance is greatly appreciated.  This is very urgent.  Thank you so much

Attached is my startup.cs code
 
                    @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>
                    }

Here is my saml parsed code

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;
            
            }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros