Create cookie when logging in

Brent Theunckens asked on March 24, 2016 11:43

I am trying to create a cookie when a user log in on logon form.

I created a new Logon Form in Visual Studio(a copy from LogonForm). In this method I am trying to create a cookie when the user is authenticated:

void Login1_LoggedIn(object sender, EventArgs e)`{

 var cookie = new HttpCookie(ConfigurationManager.AppSettings["LoggedInUserCookieName"])
    {
        Domain = ConfigurationManager.AppSettings["CookieDomain"],
        Expires = DateTime.Now.AddDays(7),
        Value = "testa"
    };
    CookieHelper.SetValue(cookie.Name, cookie.Value, cookie.Path, DateTime.Now.AddDays(7), false);
    //HttpContext.Current.Response.SetCookie(cookie);

When I log in to the new custom web part it won't create the new cookie. Can someone help me and tell me what I'm doing wrong?

Greetings, Brent

Correct Answer

Brent Theunckens answered on March 24, 2016 16:36

Hello,

I forgot to change the name in the new file:

%@ Control Language="C#" AutoEventWireup="true" Inherits="CMSWebParts_Membership_Logon_LogonForm"
 Codebehind="~/CMSWebParts/Membership/Logon/LogonForm.ascx.cs" %>

So the code was calling the original web part instead of the custom one... It's working fine now.

Thanks for the help!

0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on March 24, 2016 14:33

I can remember I had issue while using Set Cookie web part, which, most likely, uses the same CoockieHelper. I ended up using javascript to set cookies, which would be a workaround in your case. Also you could use .net to do it.

1 votesVote for this answer Mark as a Correct answer

Brent Theunckens answered on March 24, 2016 15:08 (last edited on March 24, 2016 15:36)

I'm not using the cookie web part, I'm trying to create a cookie in the CMS\CMSWebParts\Membership\Logon\LogonForm.ascx.cs file in Visual Studio. When I use HttpContext.Current.Response.SetCookie(cookie); it doesn't work either. And before the cookie is created I should use our own library to encrypt the cookie.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 24, 2016 16:01

What is the value of ConfigurationManager.AppSettings["CookieDomain"]? Does it match with your domain?

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.