public AntiForgeryRequestTokens GetAntiForgeryTokens()
        {
            if (_antiForgeryRequestTokens == null)
            {
                _antiForgeryRequestTokens = AntiForgeryRequestTokens.GetFromApplication(this);
            }

            return(_antiForgeryRequestTokens);
        }
Example #2
0
        private HttpResponseMessage SendRequest()
        {
            var requestBuilder = new RequestBuilder(_app.Server, _path);

            if (_method == HttpMethod.Post)
            {
                var tokens = AntiForgeryRequestTokens.GetFromApplication(_app);
                if (_postForm != null)
                {
                    var dic = new Dictionary <string, string>(_postForm);

                    requestBuilder.And(req =>
                    {
                        dic["__RequestVerificationToken"] = tokens.VerificationToken;
                        var content = new FormUrlEncodedContent(dic);
                        content.Headers.ContentType = new MediaTypeHeaderValue(_contentType);
                        req.Content = content;
                    })
                    .WithCookie(tokens.Cookie);
                }
                else
                {
                    requestBuilder.AddHeader("RequestVerificationToken", tokens.VerificationToken);
                    if (_postEntity != null)
                    {
                        requestBuilder.And(req =>
                        {
                            var content = new ByteArrayContent(_postEntity);
                            content.Headers.ContentType = new MediaTypeHeaderValue(_contentType);
                            req.Content = content;
                        });
                    }
                    requestBuilder.WithCookie(tokens.Cookie);
                }
            }

            if (_headers.Any())
            {
                _headers.Keys.ToList().ForEach(key => requestBuilder.AddHeader(key, _headers[key]));
            }

            if (_cookies.Any())
            {
                _cookies.ForEach(c => requestBuilder.WithCookie(c.Name.ToString(), c.Value.ToString()));
            }

            var response = requestBuilder.SendAsync(_method.ToString().ToUpper()).Result;

            _app.ResetUser();
            return(response);
        }