Good day. There is a first and second site. You need to access the protected method in the first, using the standard form authorization mechanism. WebClient on the second site makes a request for authorization, sending a username / password, a cookie comes to him, he saves it and sends a second request to a protected method with a cookie. In the second query, AUTH is visible to the cookie and it is valid, but authorization fails.
Request.isauthenticated = false What can be wrong?
Web Client Request Code
//Create an instance of your new CookieAware Web Client var client = new CookieAwareWebClient(); //Authenticate (username and password can be either hard-coded or pulled from a settings area) var values = new NameValueCollection { { "Name", "name" }, { "Password", "1234" } }; //Perform authentication - after this has been performed the cookie will be stored within the Web Client client.UploadValues(new Uri("http://localhost:15536/Plugins/ProductListGetter/login"), "POST", values); var _cookies = client.ResponseHeaders["Set-Cookie"]; client.UploadString(new Uri("http://localhost:15536/Plugins/ProductListGetter/ChangeNameForCurrentUser"), "POST", "Example Message"); client.UploadString(new Uri("http://localhost:15536/Plugins/ProductListGetter/ChangeNameForCurrentUser"), "POST", "Example Message"); client.Dispose(); Modified web client code to support cookies
public class CookieAwareWebClient : WebClient { //Properties to handle implementing a timeout private int? _timeout = null; public int? Timeout { get { return _timeout; } set { _timeout = value; } } //A CookieContainer class to house the Cookie once it is contained within one of the Requests public CookieContainer CookieContainer { get; private set; } //Constructor public CookieAwareWebClient() { CookieContainer = new CookieContainer(); } //Method to handle setting the optional timeout (in milliseconds) public void SetTimeout(int timeout) { _timeout = timeout; } //This handles using and storing the Cookie information as well as managing the Request timeout protected override WebRequest GetWebRequest(Uri address) { //Handles the CookieContainer var request = (HttpWebRequest)base.GetWebRequest(address); request.CookieContainer = CookieContainer; //Sets the Timeout if it exists if (_timeout.HasValue) { request.Timeout = _timeout.Value; } return request; }