Example #1
0
        public IActionResult AuthReqest(AuthRequestModel model)
        {
            XmlDocument request = new XmlDocument();

            request.LoadXml(Encoding.UTF8.GetString(Convert.FromBase64String(model.SAMLRequest)));

            XmlDocument doc11 = new XmlDocument();

            doc11.LoadXml(Encoding.UTF8.GetString(Convert.FromBase64String(model.SAMLRequest)));
            using (XmlTextWriter xmltw = new XmlTextWriter("C:\\Users\\Matvey\\source\\repos\\AuthApp\\AuthApp\\exampleRequest.xml", new UTF8Encoding(false)))
            {
                doc11.WriteTo(xmltw);

                xmltw.Close();
            }

            SamlRequestReader req      = new SamlRequestReader(request);
            XmlDocument       response = ResponseTool.CreateResponse(req);



            XmlDocument doc = Class2.SignXml(response.OuterXml, Class2.samlCertificate, Class2.privateKey);



            string responseBase64 = Convert.ToBase64String(Class2.StringToByteArray(doc.OuterXml));
            string url            = req.GetAssertionConsumerServiceURL();


            Response.Clear();
            StringBuilder sb = new StringBuilder();

            sb.Append("<html>");
            sb.AppendFormat(@"<body onload='document.forms[""form""].submit()'>");
            sb.AppendFormat("<form name='form' action='{0}' method='post'>", url);
            sb.AppendFormat("<input type='hidden' name='SAMLResponse' value='{0}'>", responseBase64);
            // Other params go here
            sb.Append("</form>");
            sb.Append("</body>");
            sb.Append("</html>");

            Response.WriteAsync(sb.ToString());

            return(null);
        }
Example #2
0
        public static XmlDocument CreateResponse(SamlRequestReader req)
        {
            //SamlRequestReader req = new SamlRequestReader(request);
            XmlDocument response = new SamlResponseXMLBuilder()
                                   .SetInResponseTo(req.GetAuthnRequestID())
                                   .SetDestination(req.GetAssertionConsumerServiceURL())
                                   .SetIssuer(req.GetIssuer())
                                   .SetNameID(req.GetNameID())
                                   .SetAudience(req.GetIssuer())
                                   .SetNotOnAfter(DateTime.Now.AddDays(365).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"))
                                   .SetIdResponse("_" + System.Guid.NewGuid().ToString())
                                   .SetIdAssertion("_" + System.Guid.NewGuid().ToString())
                                   .SetNotBefore(DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"))
                                   .SetSessionIndex("_samlong_")
                                   .SetIssueInstantAssertion(DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"))
                                   .SetIssueInstantResponse(DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"))
                                   .SetSessionNotOnOrAfter(DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"))
                                   .Build();


            return(response);
        }