Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

forms authentication question 1

Status
Not open for further replies.

lfc77

Programmer
Joined
Aug 12, 2003
Messages
218
Location
GB
I'm using forms authentication and on my first page I want to check if
the user has a cookie, and if they do I want to authenticate them and
send them to my default page with a greeting, otherwise send them to a
login page.

I've seen a lot of examples on the internet where the user login details
are checked and then RedirectFromLoginPage is used, but I haven't seen
anything on simply authenticating the user and then sending them to a
page of your choosing.
I can check the user is valid against my database and then redirect
them, but they are not authenticated by the application and still can't
view my pages that require authentication.

Can anybody tell me how this is done?


Thanks,

lfc77
 
Code:
string userName = <logic to get user name>;
FormsAuthentication.SetAuthCookie(userName, false);
Response.Redirect("myPage.aspx", false);
 
LV,

On my opening page I am currently checking for a cookie like this, looking for a particular cookie :

if (Request.Cookies["CookieName"] != null)
{
}

In this cookie I have an ID number which I then use to get the users details from my database.

Using SetAuthCookie then creates a second cookie, without a name so you can't check for it in the future. Is this the best way to do this? Can you give authcookie a name and just use the 1 cookie?

if (Request.Cookies["CallUK"] != null)
{
//get user details from db
FormsAuthentication.SetAuthCookie(strCookieValue, false);
Response.Redirect("default_user.aspx", false);
}
else
{
Response.Redirect("default_new_user.aspx");
}
 
The SetAuthCookie method just creates an authentication ticket with supplied user name (or say an ID like in your case), w/out redirecting. Basicaly, if you look at the Request object before executing, you'll see that Request.IsAuthenticated is "false", and after executing SetAuthCookie Request.IsAuthenticated will be set to "true". So you can check for it throughout you application to make sure that the user is authenticated in:
Code:
if(Request.IsAuthenticated)
{
  //valid user
}
You can supply your ID number stored in cookies as a parameter:
Code:
if(Request.Cookies["CookieName"] != null) 
{
  string theId = Request.Cookies["CookieName"].ToString();
  FormsAuthentication.SetAuthCookie(myId, false);
  Response.Redirect("somePage.aspx");
}
else
{
  Response.Redirect("default_new_user.aspx");
}
and get a reference to it later:
Code:
 string theId = Page.User.Identity.Name;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top