I am retrieving the tenant name from the url. I'd prefer to do it only once, store it in the cookie, and retrieve it from there when I need it in a new page request.
I am using the code below to "create" a cookie. I was hoping that the interface would allow me to store additional information but it doesn't. Is there a way to do this or am I on the wrong track?
public void SignIn(string userName, bool createPersistentCookie)
{
if (String.IsNullOrEmpty(userName))
throw new ArgumentException("Value cannot be null or empty.", "userName");
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
}
thanks in advance.
The SetAuthCookie method adds a forms-authentication ticket to either the cookies collection, or to the URL if CookiesSupported is false . The forms-authentication ticket supplies forms-authentication information to the next request made by the browser.
The ASPXAUTH cookie is used to determine if a user is authenticated. As far as the location of the cookie, that depends on your browser. If you are using Firefox you can view the cookie by clicking on Tools -> Options -> Privacy.
Form Authentication is a token-based system. When users log in, they receive a token with user information that is stored in an encrypted cookie. When a user requests an ASP.NET page via the browser, the ASP.NET verifies whether the form authentication token is available.
Cookies are small text files stored in a web user's browser. The cookies used by WS Form contain no identifiable information and are used to personalize a users experience when completing forms.
The FormsAuthenticationExtensions project on codeplex and on Nuget does exactly this. https://archive.codeplex.com/?p=formsauthext
Usage -Setting Values
using FormsAuthenticationExtensions;
using System.Collections.Specialized;
var ticketData = new NameValueCollection
{
{ "name", user.FullName },
{ "emailAddress", user.EmailAddress }
};
new FormsAuthentication().SetAuthCookie(user.UserId, true, ticketData);
Usage -Retrieving Values
using FormsAuthenticationExtensions;
using System.Web.Security;
var ticketData = ((FormsIdentity) HttpContext.Current.User.Identity).Ticket.GetStructuredUserData();
var name = ticketData["name"];
var emailAddress = ticketData["emailAddress"];
Basically, you can append a name/value dictionary inside of your FormsAuthentication cookie to store some frequently used values. We leverage this store store a small subset of user information such as companyId, etc.
Additionally, there is no 'black magic' happening here, it is simply encapsulating the setting/retrieving of the UserData property inside of the FormsAuthentication Ticket
As for consideration, please be sure to read the notes at the bottom of the project page as it describes why this should only be used for small amounts of long-living data.
Personally, I wouldn't try to alter the Auth Cookie. Instead, create a new cookie:
var myCookie = new HttpCookie("myCookie");//instantiate an new cookie and give it a name
myCookie.Values.Add("TenantName", "myTenantName");//populate it with key, value pairs
Response.Cookies.Add(myCookie);//add it to the client
Then you can read the value on that's written to the cookie like this
var cookie = Request.Cookies["myCookie"];
var tenantName = cookie.Values["TenantName"].ToString();
//tenantName = "myTenantName"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With