FormsAuthentication problem with HttpWebRequest
From: David Adams (mel1daa@hotmail.com)
Date: 01/28/03
- Next message: sergei: "Logging off user"
- Previous message: Nicole Calinoiu: "Re: Log out function in ASP.NET?"
- Next in thread: David Adams: "Re: FormsAuthentication problem with HttpWebRequest"
- Reply: David Adams: "Re: FormsAuthentication problem with HttpWebRequest"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
From: "David Adams" <mel1daa@hotmail.com> Date: Mon, 27 Jan 2003 23:01:35 -0600
Hi All,
I'm having a problem getting an ASP.NET Forms Authentication application to
work properly. I am following the IBuySpy portal security logic and using
role-based security. I have a windows app that will run and execute a page
from my application. This app I would like to automatically 1) login with
specified credentials and attain an administration role using
HttpWebRequest/HttpWebResponse, and 2) once acquiring these credentials, be
able to execute another page, which is protected in my web.config to allow
only roles of Administration.
The problem is that apparently once you set the
FormsAuthentication.SetAuthCookie, you must postback or redirect for the
global.asax Application_AuthenticateRequest to assign a new generic
principal object - I can't seem to "SetAuthCookie" and apply it to the
WebRequest Cookie collection. I am trying to do this all at once but my
asp.net app that I wish to execute does not pick up these credentials
(redirects back to login).
Here is the code: (Windows app)
// set the authentication cookie
FormsAuthentication.SetAuthCookie("dave@njs.com",false);
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.Add(new
Cookie("Roles","Admin","/",www.mysite.com"));
webRequest.CookieContainer.Add(new
Cookie(FormsAuthentication.FormsCookieName,Response.Cookies[FormsAuthenticat
ion.FormsCookieName].Value,"/",www.mysite.com"));
ds.WriteXml(sWriter,XmlWriteMode.WriteSchema);
byte[] xmlBuffer =
System.Text.Encoding.GetEncoding(1252).GetBytes(sWriter.ToString());
webRequest.Method="POST";
webRequest.ContentType = "text/xml";
webRequest.ContentLength=xmlBuffer.Length;
sm = webRequest.GetRequestStream();
sm.Write(xmlBuffer,0,xmlBuffer.Length);
webResponse=(HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode== HttpStatusCode.OK)
{
sring pageContent=smReader.ReadToEnd();
smReader.Close();
webResponse.Close();
}
GLOBAL.ASAX (from Web App)
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
// create a user role
if (Request.IsAuthenticated == true &&
Context.Request.Cookies["Roles"].Value != null &&
Context.Request.Cookies["Roles"].Value != "")
{
//this is a global routine on
ArrayList userRoles = new ArrayList();
foreach (String role in Context.Request.Cookies["Roles"].Value.Split( new
char[] {'*'} ))
{
userRoles.Add(role);
}
string[] roles = (String[]) userRoles.ToArray(typeof(String));
Context.User = new GenericPrincipal(Context.User.Identity, roles);
}
}
--------------------------
Thanks for your help!!
Dave
- Next message: sergei: "Logging off user"
- Previous message: Nicole Calinoiu: "Re: Log out function in ASP.NET?"
- Next in thread: David Adams: "Re: FormsAuthentication problem with HttpWebRequest"
- Reply: David Adams: "Re: FormsAuthentication problem with HttpWebRequest"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|