Пример #1
0
        public ActionResult CASResponse()
        {
            string response = Request.QueryString["response"];

            byte[] data          = Convert.FromBase64String(response.Replace(' ', '+'));
            string decodedString = Regex.Replace(Encoding.UTF8.GetString(data), @"\t|\n|\r|:", "");

            string casUser, casUid, casMail, casName, casLastname;
            int    casUsosId;


            using (XmlReader reader = XmlReader.Create(new StringReader(decodedString)))
            {
                reader.ReadToFollowing("casuser");
                casUser = reader.ReadElementContentAsString();
                reader.ReadToFollowing("casuid");
                casUid = reader.ReadElementContentAsString();
                reader.ReadToFollowing("casmail");
                casMail = reader.ReadElementContentAsString();
                reader.ReadToFollowing("casusos_id");
                casUsosId = StringLibrary.GetNumberFromString(reader.ReadElementContentAsString());
                // reader.ReadToFollowing("casemployeetype");
                // output.AppendLine($"Employee Type: {reader.ReadElementContentAsString()}<br />");
                // reader.ReadToFollowing("casregisteredaddress");

                // reader.ReadToFollowing("casdepartmentnumber");
                // output.AppendLine($"Department Number: {reader.ReadElementContentAsString()}<br />");
                reader.ReadToFollowing("casgivenname");
                casName = reader.ReadElementContentAsString();
                // output.AppendLine($"Imię: {reader.ReadElementContentAsString()}<br />");
                reader.ReadToFollowing("cassn");
                casLastname = reader.ReadElementContentAsString();
                // output.AppendLine($"Nazwisko: {reader.ReadElementContentAsString()}<br />");
            }

            using (var db = new DatabaseContext())
            {
                var searchUser = db.Uzytkownik.FirstOrDefault(t => t.UsosId == casUsosId);
                if (searchUser == default(Uzytkownik))
                {
                    Uzytkownik user = new Uzytkownik();
                    user.Login    = casUser;
                    user.Salt     = null;
                    user.Haslo    = null;
                    user.UsosId   = casUsosId;
                    user.Email    = casMail;
                    user.Imie     = casName;
                    user.Nazwisko = casLastname;

                    user.DataUtworzenia  = DateTime.Now;
                    user.DataModyfikacji = DateTime.Now;
                    user.IDP             = 2;
                    user.Rola            = 0;

                    db.Uzytkownik.Add(user);
                    db.SaveChanges();

                    FormsAuthentication.SetAuthCookie(user.Login, false);

                    Auth.Repository.LogIn(HttpContext.Response, user);

                    /*var authTicket = new FormsAuthenticationTicket(1, user.Login, DateTime.Now, DateTime.Now.AddMinutes(20), false, "");
                     * string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                     * var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                     * HttpContext.Response.Cookies.Add(authCookie);*/
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    /*FormsAuthentication.SetAuthCookie(searchUser.Login, false);
                     *
                     * var authTicket = new FormsAuthenticationTicket(1, searchUser.Login, DateTime.Now, DateTime.Now.AddMinutes(20), false, "");
                     * string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                     * var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                     * HttpContext.Response.Cookies.Add(authCookie);*/
                    Auth.Repository.LogIn(HttpContext.Response, searchUser);
                    return(RedirectToAction("Index", "Home"));
                }
            }



            /*XmlSerializer ser = new XmlSerializer(typeof(Models.Account.casServiceResponse));
             * Models.Account.casServiceResponse casResponse = (Models.Account.casServiceResponse)ser.Deserialize(new StringReader(decodedString));*/
        }