Answered by:
[WPF Application] Send FormAuthenticationCookie with WebRequest

Question
-
Hi everyone,
I'm making an application by using WPF to connect to my website.
My website provides functions return JsonResult to my WPF application.
My problem is : Those functions I wanna use require FormAuthenticationCookie , otherwise, it will be redirected to login page.
Here is the class I store in my FormAuthenticationCookie :
public class AuthenticationModel : IAccountAuthentication { #region Public assessments. /// <summary> /// This can be understood as username. /// </summary> public string AccountName { get; set; } /// <summary> /// Password of account. /// </summary> public string AccountPassword { get; set; } /// <summary> /// Full name of this account. /// </summary> public string FullName { get; set; } /// <summary> /// Role of account. /// </summary> public AccessibleRoles Role { get; set; } public IIdentity Identity { get; } #endregion #region Public methods. /// <summary> /// 1 parameter constructor. /// </summary> public AuthenticationModel(string accountName) { // Invalid user name. if (string.IsNullOrEmpty(accountName)) return; AccountName = accountName; Identity = new GenericIdentity(accountName); } /// <summary> /// This function is useless, just for overriding function. /// </summary> /// <param name="role">Role name which is assigned to user.</param> /// <returns></returns> public bool IsInRole(string role) { return false; } #endregion }
Can anyone help me please ?
Thank you,
Tuesday, October 6, 2015 8:38 AM
Answers
-
Take a look at this thread here:
http://stackoverflow.com/questions/11762301/trying-to-get-authentication-cookies-using-httpwebrequest
public class CookiesAwareWebClient : WebClient { private CookieContainer outboundCookies = new CookieContainer(); private CookieCollection inboundCookies = new CookieCollection(); public CookieContainer OutboundCookies { get { return outboundCookies; } } public CookieCollection InboundCookies { get { return inboundCookies; } } public bool IgnoreRedirects { get; set; } protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = outboundCookies; (request as HttpWebRequest).AllowAutoRedirect = !IgnoreRedirects; } return request; } protected override WebResponse GetWebResponse(WebRequest request) { WebResponse response = base.GetWebResponse(request); if (response is HttpWebResponse) { inboundCookies = (response as HttpWebResponse).Cookies ?? inboundCookies; } return response; } }
and
public NameValueCollection LoginToDatrose() { var loginUriBuilder = new UriBuilder(); loginUriBuilder.Host = DatroseHostName; loginUriBuilder.Path = BuildURIPath(DatroseBasePath, LOGIN_PAGE); loginUriBuilder.Scheme = "https"; var postData = new NameValueCollection(); postData.Add("LoginName", DatroseUserName); postData.Add("Password", DatrosePassword); var responseCookies = new NameValueCollection(); using (var client = new CookiesAwareWebClient()) { client.IgnoreRedirects = true; var clientResponse = client.UploadValues(loginUriBuilder.Uri, "POST", postData); foreach (var nvp in client.InboundCookies.OfType<Cookie>()) { responseCookies.Add(nvp.Name, nvp.Value); } } return responseCookies; }
- Proposed as answer by Xavier Xie-MSFT Thursday, October 8, 2015 5:26 AM
- Marked as answer by Xavier Xie-MSFT Wednesday, October 21, 2015 9:37 AM
Tuesday, October 6, 2015 8:50 AM -
Of course you need to authenticate yourself with the same credentials regardless of whether you are accessing the secured website from from a browser or any other kind of application like for example a WPF application. A cookie is just a small piece of data that is stored in the user's web browser after he or she has successfully authenticated against the website at least once.
This is not a WPF topic but the WebReuqest class has a Credentials property that you can use to specify the credentials that the web site accepts:
https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx
http://www.justskins.com/forums/httpwebrequest-impersonation-and-defaultcredentials-64356.htmlHope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by Xavier Xie-MSFT Thursday, October 8, 2015 5:26 AM
- Marked as answer by Xavier Xie-MSFT Wednesday, October 21, 2015 9:37 AM
Tuesday, October 6, 2015 3:57 PM
All replies
-
Take a look at this thread here:
http://stackoverflow.com/questions/11762301/trying-to-get-authentication-cookies-using-httpwebrequest
public class CookiesAwareWebClient : WebClient { private CookieContainer outboundCookies = new CookieContainer(); private CookieCollection inboundCookies = new CookieCollection(); public CookieContainer OutboundCookies { get { return outboundCookies; } } public CookieCollection InboundCookies { get { return inboundCookies; } } public bool IgnoreRedirects { get; set; } protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = outboundCookies; (request as HttpWebRequest).AllowAutoRedirect = !IgnoreRedirects; } return request; } protected override WebResponse GetWebResponse(WebRequest request) { WebResponse response = base.GetWebResponse(request); if (response is HttpWebResponse) { inboundCookies = (response as HttpWebResponse).Cookies ?? inboundCookies; } return response; } }
and
public NameValueCollection LoginToDatrose() { var loginUriBuilder = new UriBuilder(); loginUriBuilder.Host = DatroseHostName; loginUriBuilder.Path = BuildURIPath(DatroseBasePath, LOGIN_PAGE); loginUriBuilder.Scheme = "https"; var postData = new NameValueCollection(); postData.Add("LoginName", DatroseUserName); postData.Add("Password", DatrosePassword); var responseCookies = new NameValueCollection(); using (var client = new CookiesAwareWebClient()) { client.IgnoreRedirects = true; var clientResponse = client.UploadValues(loginUriBuilder.Uri, "POST", postData); foreach (var nvp in client.InboundCookies.OfType<Cookie>()) { responseCookies.Add(nvp.Name, nvp.Value); } } return responseCookies; }
- Proposed as answer by Xavier Xie-MSFT Thursday, October 8, 2015 5:26 AM
- Marked as answer by Xavier Xie-MSFT Wednesday, October 21, 2015 9:37 AM
Tuesday, October 6, 2015 8:50 AM -
Of course you need to authenticate yourself with the same credentials regardless of whether you are accessing the secured website from from a browser or any other kind of application like for example a WPF application. A cookie is just a small piece of data that is stored in the user's web browser after he or she has successfully authenticated against the website at least once.
This is not a WPF topic but the WebReuqest class has a Credentials property that you can use to specify the credentials that the web site accepts:
https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx
http://www.justskins.com/forums/httpwebrequest-impersonation-and-defaultcredentials-64356.htmlHope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by Xavier Xie-MSFT Thursday, October 8, 2015 5:26 AM
- Marked as answer by Xavier Xie-MSFT Wednesday, October 21, 2015 9:37 AM
Tuesday, October 6, 2015 3:57 PM