System.Web.Security.RolePrincipal.ToEncryptedTicket C# (CSharp) Method

ToEncryptedTicket() public method

public ToEncryptedTicket ( ) : string
return string
		public string ToEncryptedTicket ()
		{
			string roles = string.Join (",", GetRoles ());
			string cookiePath = RoleManagerConfig.CookiePath;
			int approxTicketLen = roles.Length + cookiePath.Length + 64;

			if (_cachedArray.Length > Roles.MaxCachedResults)
			       return null;

			MemoryStream ticket = new MemoryStream (approxTicketLen);
			BinaryWriter writer = new BinaryWriter (ticket);

			// version
			writer.Write (Version);
		
			// issue datetime
			DateTime issueDate = DateTime.Now;
			writer.Write (issueDate.Ticks);

			// expiration datetime
			writer.Write (_expireDate.Ticks);

			writer.Write (cookiePath);
			writer.Write (roles);

			CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;

			byte[] ticket_data = ticket.GetBuffer ();
			if (cookieProtection == CookieProtection.All) {
				ticket_data = MachineKeySectionUtils.EncryptSign (MachineConfig, ticket_data);
			} else if (cookieProtection == CookieProtection.Encryption) {
				ticket_data = MachineKeySectionUtils.Encrypt (MachineConfig, ticket_data);
			} else if (cookieProtection == CookieProtection.Validation) {
				ticket_data = MachineKeySectionUtils.Sign (MachineConfig, ticket_data);
			}

			return GetBase64FromBytes (ticket_data, 0, ticket_data.Length);
		}

Usage Example

        private void OnLeave(Object source, EventArgs eventArgs)
        {
            HttpApplication app;
            HttpContext     context;

            app     = (HttpApplication)source;
            context = app.Context;

            if (!Roles.Enabled || !Roles.CacheRolesInCookie || context.Response.HeadersWritten)
            {
                return;
            }

            if (context.User == null || !(context.User is RolePrincipal) || !context.User.Identity.IsAuthenticated)
            {
                return;
            }
            if (Roles.CookieRequireSSL && !context.Request.IsSecureConnection)
            { // if cookie is sent, then clear it
                if (context.Request.Cookies[Roles.CookieName] != null)
                {
                    Roles.DeleteCookie();
                }
                return;
            }
            RolePrincipal rp = (RolePrincipal)context.User;

            if (rp.CachedListChanged && context.Request.Browser.Cookies)
            {
                string s = rp.ToEncryptedTicket();
                if (string.IsNullOrEmpty(s) || s.Length > MAX_COOKIE_LENGTH)
                {
                    Roles.DeleteCookie();
                }
                else
                {
                    HttpCookie cookie = new HttpCookie(Roles.CookieName, s);
                    cookie.HttpOnly = true;
                    cookie.Path     = Roles.CookiePath;
                    cookie.Domain   = Roles.Domain;
                    if (Roles.CreatePersistentCookie)
                    {
                        cookie.Expires = rp.ExpireDate;
                    }
                    cookie.Secure = Roles.CookieRequireSSL;
                    context.Response.Cookies.Add(cookie);
                }
            }
        }
All Usage Examples Of System.Web.Security.RolePrincipal::ToEncryptedTicket