static NameValueCollection Map(object values, AntiForgeryTokenViewModel xsrf = null)
        {
            var coll = values as NameValueCollection;

            if (coll != null)
            {
                return(coll);
            }

            coll = new NameValueCollection();
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
            {
                var val = descriptor.GetValue(values);
                if (val == null)
                {
                    val = "";
                }
                coll.Add(descriptor.Name, val.ToString());
            }

            if (xsrf != null)
            {
                coll.Add(xsrf.Name, xsrf.Value);
            }

            return(coll);
        }
Ejemplo n.º 2
0
 protected void ProcessXsrf(HttpResponseMessage resp)
 {
     if (resp.IsSuccessStatusCode)
     {
         var model = resp.GetModel <LoginViewModel>();
         if (model.AntiForgery != null)
         {
             Xsrf = model.AntiForgery;
             var cookies = resp.GetCookies().Where(x => x.Name == Xsrf.Name);
             client.SetCookies(cookies);
         }
     }
 }
        public static HttpResponseMessage PostForm(this IdentityServerHost host, string path, object value, AntiForgeryTokenViewModel xsrf = null)
        {
            if (!path.StartsWith("http"))
            {
                path = host.Url.EnsureTrailingSlash() + path;
            }

            var form    = Map(value, xsrf);
            var body    = ToFormBody(form);
            var content = new StringContent(body, Encoding.UTF8, FormUrlEncodedMediaTypeFormatter.DefaultMediaType.MediaType);

            var response = host.Client.PostAsync(path, content).Result;

            host.ProcessCookies(response);

            return(response);
        }