I have two browsers from two different machines hitting a test page. The test page is retrieving a cookie. If one is not there, it creates a cookie.
public string GetUserCookieId()
{
string cookieName = "CookieId3";
HttpCookie userInfoCookies = Request.Cookies[cookieName];
string cookieId = "";
if (userInfoCookies != null)
cookieId = userInfoCookies.Value;
if (string.IsNullOrEmpty(cookieId))
{
cookieId = Guid.NewGuid().ToString();
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = cookieId;
cookie.Expires = DateTime.Now.AddDays(90);
Response.SetCookie(cookie);
}
return cookieId;
}
Both browser machines are displaying the same cookie value on the page. This even once I have renamed the cookie to "CookieId3". Please tell me where i am going wrong.
As you can see, the method is not static. Thank you
The issue appears to be the difference between Request and HttpContext.Current.Request when the code is accessed from a ClassLibrary.
Is anyone is able to shed some light on this?!
The following works.
public string GetUserCookieId()
{
string cookieName = "CookieId18";
HttpCookie userInfoCookies = HttpContext.Current.Request.Cookies[cookieName];
string cookieId = "";
if (userInfoCookies != null)
cookieId = userInfoCookies.Value;
if (string.IsNullOrEmpty(cookieId))
{
cookieId = Guid.NewGuid().ToString();
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value = cookieId;
cookie.Expires = DateTime.Now.AddDays(90);
HttpContext.Current.Response.SetCookie(cookie);
}
return cookieId;
}
Related
string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
if (Request.Cookies["UserSettings"] != null)
{
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie);
}
i am new to this authentication mechanism and still learning.What i am trying to achieve is that..when the user tries to use multiple instances of the application like say for by opening different tabs,the application should automatically logoff.
My login Code
public static bool SignIn(int officeId, string userName, string password, bool remember, System.Web.UI.Page page)
{
int userId = BusinessLayer.Office.Users.GetUserId(userName);
bool active = BusinessLayer.Office.Users.IsUserActive(userId);
if(!active)
{
return false;
}
if (page != null)
{
try
{
string remoteAddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
string remoteUser = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_USER"];
DateTime? serverDate = BusinessLayer.Core.DateConversion.GetCurrentServerDate();
string pass = BusinessLayer.Security.Password.GetHash(password, userName);
long logOnId = Everest.Net.DatabaseLayer.Security.User.SignIn(officeId, userName,
BusinessLayer.Security.Password.GetHash(password, userName),
page.Request.UserAgent, remoteAddress, remoteUser);
// var isActive = BusinessLayer.Office.Users.GetUserId(userName);
if (logOnId > 0)
{
if (IsBoDStarted(Conversion.TryCastDate(serverDate)))
{
var isSessionSet = SetSession(page, userName);
if (isSessionSet)
{
var ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
DateTime.Now.AddHours(12), remember, String.Empty, FormsAuthentication.FormsCookiePath);
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
cookie.Expires = DateTime.Now.AddHours(12);
page.Response.Cookies.Add(cookie);
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(userName, true, "Everest.Net");
return true;
}
}
else
{
var ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
DateTime.Now.AddHours(12), remember, String.Empty, FormsAuthentication.FormsCookiePath);
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
cookie.Expires = DateTime.Now.AddHours(12);
page.Response.Cookies.Add(cookie);
page.Response.Redirect("~/Utilities/BodOperation.aspx");
return true;
}
}
}
catch (DbException)
{
//Swallow the exception
return false;
}
}
if (page != null)
{
page.Session.Clear();
}
return false;
}
Any ideas by you experts around here would be helpful.I did searched about this in SO and in here.Couldnt get the grasp of it.Any help appreciated.
My webconfig
<authentication mode="Forms">
<forms loginUrl="~/SignIn.aspx" timeout="6" slidingExpiration="true" defaultUrl="~/Index.aspx" />
</authentication>
In my above webconfig since the timeout is set to 6minutes, this doesnot works too.
Help Please.
do you know how cookies works on ASP.NET? could you tell me?
and how to call the cookies to another page?
i have login form, and i use cookies. but i can't call that cookies to another page. i want to use some data from login form (it's like domain name, username and password) to do change password from changepassword.aspx form.
somebody please help me.
void Login_Click(object sender, EventArgs e)
{
string adPath = "LDAP://mydomain.com"; //Path to your LDAP directory server
LdapAuthentication adAuth = new LdapAuthentication(adPath);
try
{
if(true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
{
//string groups = adAuth.GetGroups();
string groups = txtUsername.Text;
//Create the ticket, and add the groups.
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);
//Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
//Create a cookie, and then add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
if(true == isCookiePersistent)
authCookie.Expires = authTicket.Expiration;
//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);
//You can redirect now.
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
}
else
{
errorLabel.Text = "Authentication did not succeed. Check user name and password.";
}
}
catch(Exception ex)
{
errorLabel.Text = "Error authenticating. " + ex.Message;
}
}
</script>
this is how use cookies in login form.
how can i use cookies in change password form?
I have the following code:
var serializer = new XmlSerializer(typeof(classes.Member));
TextWriter writer = new StringWriter();
serializer.Serialize(writer, active_resident);
HttpCookie cookie = Request.Cookies["ActiveUser"];
if (cookie == null)
{
cookie = new HttpCookie("ActiveUser");
}
cookie.Expires = DateTime.Now.AddHours(1);
cookie.Value = Genesis.Encryption.EncryptAES(writer.ToString(), Genesis.Generic.ReadAppSettingsValue("GenesisEncryptionKey"));
Response.Cookies.Add(cookie);
writer.Close();
// Redirect to Secure Area
Response.Redirect("secure/dashboard.aspx", false);
Strangely, the cookie is not written? However, the ASP.Net Session cookie is? Any ideas?
I tried
if (Request.Cookies["IsGuest"] != null)
{
Response.Cookies["IsGuest"].Expires = DateTime.Now.AddDays(-1);
//HttpCookie myCookie = new HttpCookie("IsGuest");
//myCookie.Expires = DateTime.Now.AddDays(-1d);
//Response.Cookies.Add(myCookie);
}
string a = Request.Cookies["IsGuest"].Value;
But Request.Cookies["IsGuest"] is never null.
Commenting uncommented code and doing vice versa also did not help.
Try this:
if (Request.Cookies["IsGuest"] != null || Request.Cookies["IsGuest"].Value != "")
{
Response.Cookies["IsGuest"].Expires = DateTime.Now.AddDays(-1);
//HttpCookie myCookie = new HttpCookie("IsGuest");
//myCookie.Expires = DateTime.Now.AddDays(-1d);
//Response.Cookies.Add(myCookie);
}
string a = Request.Cookies["IsGuest"].Value;
I think your Cookie exists but it has no value.
If it's not work, look whats in string a and than you can compare it in the first row.