Reputation: 66445
I wanted to know if it was possible to keep the Session variable thought builds in a ASP.NET + C#?
I ask this because every time I make a minor change to my application and need to rebuild it, I need to login again and do a nunch of operation after that... it's taking a lot of my time.
If there is no way around I can set up a testing mode where I'll always be logged in, or automatize the log in procedure... but it would save me time to just keep the Session after a build.
Upvotes: 2
Views: 1147
Reputation: 11699
This answer is community wiki so as not to generate any reputation, as it's effectively a reworking of DOK's answer. If you like it, please upvote DOK's answer.
@Dok, if you want to edit your answer to incorporate any of this, please do, and I'll gladly delete this answer. :)
DOK, as mentioned in my comments to your answer (and possibly some help for your own solution), you might want to do the following:
#if DEBUG //As mentioned by DOK in the comments. If you set debug to false when building for deployment, the code in here will not be compiled.
protected void Page_PreInit(object sender, EventArgs e)
{
bool inDevMode = false;
inDevMode = bool.Parse(ConfigurationManager.AppSettings["InDevMode"]); //Or you could use TryParse
if(inDevMode)
{
// Fake authentication so I don't have to create a damn Login page just for this.
System.Web.Security.FormsIdentity id = new FormsIdentity(new FormsAuthenticationTicket("dok", false, 30));
string[] roles = { "a" };
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
#endif
To further ensure that you do not accidentally deploy with this active, then you would have your app settings in seperate config files (as well as your debug section). If you use Web Deployment projects, then you can put your dev config settings in one file, and your live config files in another (this is usually dev.config and live.config!).
eg, in your web.config:
<appSettings file="dev.config"/>
In your dev.config:
<appSettings>
<add key="InDevMode" value="true" />
</appSettings>
In your live.config:
<appSettings>
<add key="InDevMode" value="false" />
</appSettings>
Upvotes: 0
Reputation: 32841
I have used this hack when I didn't want to deal with authentication during development:
protected void Page_PreInit(object sender, EventArgs e)
{
// Fake authentication so I don't have to create a damn Login page just for this.
System.Web.Security.FormsIdentity id = new FormsIdentity(new FormsAuthenticationTicket("dok", false, 30));
string[] roles = { "a" };
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
This is only going to work on the page you put it on, though you could add it to a base page.
You definitely have to remember to remove this before you promote the code to test/QA/UAT/prod!
Upvotes: 3
Reputation: 53125
You could change your test server to use the State Server or SQL Server session state modes, which will survive an application restart.
Upvotes: 6