public static void SetAuthCookie(string userName, bool createPersistentCookie) { var authCookie = new OpenNETCF.Web.HttpCookie(FormsCookieName); authCookie.Values["UID"] = userName; authCookie.Domain = FormsAuthentication.CookieDomain; authCookie.Expires = DateTime.Now.Add(FormsAuthentication.Timeout); if (createPersistentCookie) { } HttpContext.Current.Response.SetCookie(authCookie); }
private void DisplayAuthenticated(bool viaCookie, string UID) { //This will display to the user a few pieces of information about the session data stored Document doc = new Document(); doc.Head = new DocumentHead("OpenNETCF Padarn Web Server", new StyleInfo("css/SampleSite.css")); Utility.AddPadarnHeaderToDocument(doc, true, "Execute"); Div containerDiv = new Div("container"); Form form = new Form("CookieWork.aspx", FormMethod.Post); if (viaCookie) { form.Add(new RawText(String.Format("Your are authenticated via cookie"))); } else { form.Add(new RawText(String.Format("Your are authenticated via normal credentials"))); } form.Add(new LineBreak()); form.Add(new RawText(String.Format("Cookie last visit on {0}", Request.Cookies[COOKIENAME][COOKIEVAL_LAST_VISIT] == null ? "NEVER" : DateTime.Parse(Request.Cookies[COOKIENAME][COOKIEVAL_LAST_VISIT]).ToString()))); form.Add(new LineBreak()); form.Add(new RawText(String.Format("Cookie GUID is {0}", Request.Cookies[COOKIENAME][COOKIEVAL_GUID]))); //Let the user log out at any point form.Add(new LineBreak()); form.Add(new LineBreak()); form.Add(new Button(new ButtonInfo(ButtonType.Submit, "Log Out"))); form.Add(new Hidden("LogOut", UID)); containerDiv.Add(form); doc.Body.Add(containerDiv); if (Request.Cookies.Count > 0 && Request.Cookies[COOKIENAME] != null) { //Modify cookie to show we've been here before OpenNETCF.Web.HttpCookie myCookie = Request.Cookies[0]; myCookie.Values[COOKIEVAL_LAST_VISIT] = DateTime.Now.ToString(); Response.SetCookie(myCookie); } //Write out the response Response.Write(doc.OuterHtml); Response.Flush(); }
protected override void Page_Load(object sender, EventArgs e) { if (Request.Form.Count > 0) { if (Request.Form["Submit"] != null) { string UID = Request.Form["UserName"]; string PW = Request.Form["PW"]; Guid uoGuid = Authentication.ValidateUser(UID, PW); if (uoGuid != Guid.Empty) { //1. Set GUID in Cookie OpenNETCF.Web.HttpCookie myCookie = new OpenNETCF.Web.HttpCookie(COOKIENAME); myCookie.Values[COOKIEVAL_USER_NAME] = UID; myCookie.Values[COOKIEVAL_GUID] = uoGuid.ToString(); myCookie.Values[COOKIEVAL_LAST_VISIT] = DateTime.Now.ToString(); myCookie.Expires = DateTime.Now.AddDays(1); if (Request.Browser.Cookies) { //2. Verify browser can accept cookies Response.SetCookie(myCookie); } //3. Do redirect to authenticated page Response.Redirect("CookieWork.aspx"); } else { //Show bad credentials screen DisplayLoginForm(true); } } else if (Request.Form["LogOut"] != null) { //1. Perform log-out string UID = Request.Form["LogOut"]; Authentication.LogOff(UID); //2. Delete cookie if (Request.Cookies[COOKIENAME] != null) { OpenNETCF.Web.HttpCookie myCookie = Request.Cookies[COOKIENAME]; myCookie.Expires = DateTime.Now.AddDays(-1); if (Request.Browser.Cookies) { //Verify browser can accept cookies Response.SetCookie(myCookie); } } //3. Refresh page Response.Redirect("CookieWork.aspx"); } } else { //Check GUID in cookie; redirect to authenticated page if so if (Request.Cookies[COOKIENAME] != null && Request.Cookies[COOKIENAME].HasKeys) { string guidCookie = Request.Cookies[COOKIENAME].Values[COOKIEVAL_GUID]; Guid g = new Guid(guidCookie); Authentication.UserObject uo; if (!(uo = Authentication.ValidateUser(g)).Null && !uo.IsGuidExpired()) { if (Request.Cookies[COOKIENAME][COOKIEVAL_LAST_VISIT] == null) { //We got here upon manual passage of credentials DisplayAuthenticated(false, uo.UserName); } else { //We got here by reading cookie DisplayAuthenticated(true, uo.UserName); } } else { //We need to re-authenticate because GUID is invalid or expired DisplayLoginForm(false); } } else { //We need to re-authenticate; no cookie was found DisplayLoginForm(false); } } }