locked
authentication in Web API using console RRS feed

  • Question

  • User-1350516731 posted

    Webservice code

    [Authorize]
        public class RegistrationController : ApiController
        {
            [AllowAnonymous]
            [HttpPost]
            public string Get(string user,string pass)
            {
                if (user=="abc"&&pass=="cba")
                    FormsAuthentication.SetAuthCookie("HomeUser", false);
                return "Home";
            }
            [HttpGet]
            public string Post()
            {
                return "Post";
            }
        }

    Forms Authentication from console

    class CookieWebClient : WebClient
            {
                public CookieContainer CookieContainer { get; private set; }
     
                /// <summary>
                /// This will instanciate an internal CookieContainer.
                /// </summary>
                public CookieWebClient()
                {
                    this.CookieContainer = new CookieContainer();
                }
     
                /// <summary>
                /// Use this if you want to control the CookieContainer outside this class.
                /// </summary>
                public CookieWebClient(CookieContainer cookieContainer)
                {
                    this.CookieContainer = cookieContainer;
                }
     
                protected override WebRequest GetWebRequest(Uri address)
                {
                    var request = base.GetWebRequest(address) as HttpWebRequest;
                    if (request == null) return base.GetWebRequest(address);
                    request.CookieContainer = CookieContainer;
                    return request;
                }
            }
    using (var client = new CookieWebClient())
                {
                    var values = new NameValueCollection
        {
            { "user", "abc" },
            { "pass", "cba" },
        };
                    client.UploadValues("http://localhost:1401/Get/","POST", values);
     
                    // If the previous call succeeded we now have a valid authentication cookie
                    // so we could download the protected page
                    string result = client.DownloadString("http://localhost:1401");
                }

    I have an error 405 Unknown method on line UploadValues. Can you help me?

    Tuesday, June 30, 2015 7:13 AM

Answers

  • User-782957977 posted

    Try like this

    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    client.UploadValues("http://localhost:1401/Api/Registration","Post", values);

    Web Api

    public class RegistrationController : ApiController
        {
            [AllowAnonymous]
            [HttpPost]
            public string Get(FormDataCollection value)
            {
            }
    
            [HttpGet]
            public string Post()
            {
            }
        }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 30, 2015 8:19 PM