Create Forms Authentication Ticket with Machine Keys



repost, no ms response

I'm using Selenium to test a web application.
I need to create a Forms Authentication Cookie and let Selenium load it into
the browser instance.

I'm having a problem because my nUnit class does not have access to the
web.config file.
The website uses MachineKey valdationKey and decryptionKey.
I know these values and can put them in the nUnit class.
However, I usually create Forms Cookies by doing

tkt = new FormsAuthenticationTicket(1, txtNewIdentity.Text, DateTime.Now,
DateTime.Now.AddMinutes(TimeOut_Get()), bPersistent,

CookieValue= FormsAuthentication.Encrypt(tkt)

I don't believe the cookie will properly encrypted because when I run
..Encrypt(tkt), it won't find the encryption key to use.

Any way to manually make the forms authentication cookie without assuming
the .net methods have access to the web.config file?



Steven Cheng


from your description, you're doing some unit test on an ASP.NET
webapplication and find that the custom machinekey (stored in web.config
file) is not available in your UNIT test program, correct?

As you find, The FormsAuthentication.Encrypt class has encapsulated all the
underlying cookie encryption(also decryption ) details internally so that
we do not have access to the underlying web.config(machinekey) processing.
If you want to make the built-in FormsAuthentication API work, you may need
to run the unit test code in a web application(web application hosted in VS
test server). Otherwise, you may need to manually create an ASP.NET web
applicaion host in your console application like the casini server:

#ASP. NET Client-side Hosting with Cassini


Steven Cheng

Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question