我想知道是否可以将 Session 变量思想保留在 ASP.NET + C# 中?
我问这个是因为每次我对我的应用程序进行小的更改并需要重新构建它时,我都需要再次登录并在此之后进行一些操作......这需要我很多时间。
如果没有办法,我可以设置一个我将始终登录的测试模式,或者自动化登录过程......但它会节省我在构建后保留会话的时间。
我想知道是否可以将 Session 变量思想保留在 ASP.NET + C# 中?
我问这个是因为每次我对我的应用程序进行小的更改并需要重新构建它时,我都需要再次登录并在此之后进行一些操作......这需要我很多时间。
如果没有办法,我可以设置一个我将始终登录的测试模式,或者自动化登录过程......但它会节省我在构建后保留会话的时间。
您可以更改您的测试服务器以使用State Server 或 SQL Server 会话状态模式,这将在应用程序重新启动后继续存在。
当我不想在开发过程中处理身份验证时,我使用了这个 hack:
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);
}
这仅适用于您放置它的页面,尽管您可以将其添加到基本页面。
在将代码提升到 test/QA/UAT/prod 之前,您一定要记住删除它!
这个答案是社区 wiki,以免产生任何声誉,因为它实际上是对 DOK 答案的修改。如果你喜欢它,请点赞 DOK 的回答。
@Dok,如果您想编辑答案以包含其中的任何内容,请执行此操作,我很乐意删除此答案。:)
DOK,正如我在对您的回答的评论中提到的(可能对您自己的解决方案有一些帮助),您可能需要执行以下操作:
#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
为了进一步确保您不会意外部署此活动,您将在单独的配置文件(以及您的调试部分)中拥有您的应用程序设置。如果您使用Web 部署项目,那么您可以将您的开发配置设置放在一个文件中,将您的实时配置文件放在另一个文件中(通常是 dev.config 和 live.config!)。
例如,在您的 web.config 中:
<appSettings file="dev.config"/>
在您的 dev.config 中:
<appSettings>
<add key="InDevMode" value="true" />
</appSettings>
在你的 live.config 中:
<appSettings>
<add key="InDevMode" value="false" />
</appSettings>