I am trying to get a forms auth cookie from a url using the my domain credential in my office network. When I used the url on the internet explorer, it says, I am authorized. But programmatically I am trying to get Forms Auth cookie from the Forms auth cookie url. Iam using Webclient and I am passing NetworkCredential including my domain name. But I am getting the exception as "The remote server returned an error: (401) Unauthorized". Am I missing anything here?
using (var client = new WebClientEntended())
{
if (String.IsNullOrEmpty(user) && String.IsNullOrEmpty(password))
{
client.UseDefaultCredentials = true;
}
else
{
client.Credentials = new NetworkCredential(user, password, "domain");
}
client.DownloadString(FormsAuthentication.LoginUrl);
var cookies = client.CookieContainer.GetCookies(new Uri(FormsAuthentication.LoginUrl));
foreach (var ckie in cookies)
{
if (ckie.ToString().Contains(FormsAuthentication.FormsCookieName))
{
authCookie = (Cookie)ckie;
CacheHelper.SetValue(string.Format("{0}{1}", user, FormsAuthentication.FormsCookieName),
authCookie, OneDay);
return true;
}
}
}