title: How are role cookies encrypted?
body: <p>FormsAuthentication has an encrypt and decrypt method to push and pull the Authentication Ticket. Roles has a lot of the same methods, but it does not tell you what type of encryption is being used, or how to decrypt it. Can anyone point me in the right direction? I need to be able to mock up a Role Cookie for a test.</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>Here is an example of the problem that I'm still having.</p>
  
  <pre><code>SetLoggedInUserInHttpContext(User, Roles.GetRolesForUser(User.UserID.ToString()));
  
  RQUserMembership member = new RQUserMembership();
  QUserMembership mem = member.GetByUserAndPass(User.Username, User.Password);
  
  FormsAuthentication.SetAuthCookie(mem.UserId.ToString(), true);
  QGlobals.expireLoginProxyID();
  RQLoginAttempt.LogSuccessfulAttempt(User.Username);
  </code></pre>
  
  <p>Here is the setting of the user</p>
  
  <pre><code>        public static bool SetLoggedInUserInHttpContext(QUser User, string[] roles = null) {
          if (HttpContext.Current != null) {
              if (roles == null) {
                  roles = Roles.GetRolesForUser(User.UserID.ToString());
              } 
  
              GenericIdentity genericIdentity = new GenericIdentity(User.UserID.ToString());
              RolePrincipal genericUser = new RolePrincipal(genericIdentity); //rolesToSet
              HttpContext.Current.User = genericUser;
              return (User.UserID == QGlobals.GetLoggedInUserID());
          } else {
              return false;
          }
      }
  </code></pre>
  
  <p>My attempt to get the byte[]:</p>
  
  <pre><code>        HttpContext blah = HttpContext.Current;
          string blah2 = HttpContext.Current.Request.Cookies[".ASPXROLES"].Value;
          byte[] bytes = new byte[blah2.Length * sizeof(char)];
          System.Buffer.BlockCopy(blah2.ToCharArray(), 0, bytes, 0, bytes.Length);
          byte[] blah3 = MachineKey.Unprotect(bytes);
          var str = System.Text.Encoding.Default.GetString(blah3);
  </code></pre>
  
  <p>I'm now getting an error on <code>blah3 = MachineKey.Unprotect(bytes);</code></p>
  
  <pre><code>Error occurred during a cryptographic operation.
  
     at System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(Func`2 func, Byte[] input)
     at System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.Unprotect(Byte[] protectedData)
     at System.Web.Security.MachineKey.Unprotect(ICryptoServiceProvider cryptoServiceProvider, Byte[] protectedData, String[] purposes)
     at System.Web.Security.MachineKey.Unprotect(Byte[] protectedData, String[] purposes)
     at Quorra.Repositories.RQUser.GetUserHomePageStats(Int32 UserID, Int32 HourInterval) in e:\Code\quorra\Quorra.Domain\Repositories\RQUser.cs:line 133
     at Quorra.Admin.Controllers.HomeController.Home(Nullable`1 refreshBasketCount) in e:\Code\quorra\Quorra.Admin\Controllers\HomeController.cs:line 31
     at lambda_method(Closure , ControllerBase , Object[] )
     at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
     at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;BeginInvokeSynchronousActionMethod&gt;b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
     at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;&gt;c__DisplayClass48.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__41()
  </code></pre>
  
  <p>Any direction would be appreciated.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>To clarify I need to be able to set up a role cookie for a user so that <code>Roles.IsUserInRole();</code> works. Right now if I pass the userId it works, because it goes to the role provider and runs that method, but to check the logged on user, it just tests the cookie. I don't actually need to be able to decrypt it, if I can encrypt it, that will be enough.</p>