asp.net 如何使用Microsoft.Web.Administration在IIS Web应用程序中设置会话状态- Cookie设置-超时?

nbysray5  于 2023-02-17  发布在  .NET
关注(0)|答案(1)|浏览(146)

我正在使用Microsoft. Web. Administration以编程方式在选定的父网站下创建IIS Web应用程序。我可以使用下面的代码很好地创建应用程序,但我不确定如何通过Application变量更改应用程序的此特定方面(Cookie超时值)。

Microsoft.Web.Administration.Application new_parent_app = target_trial_site.Applications.Add("/Test", new_app_path);
new_parent_app.ApplicationPoolName = "DefaultAppPool";
new_parent_app.SetAttributeValue("system.web/sessionState/timeout", 720); // what should go here? or is it a different function like SetMetadata?

下面是我尝试在IIS UI中更改的值的图片,以供参考:

这里的Microsoft's developer site也不清楚我会如何做。
提前感谢您的帮助。

c2e8gylq

c2e8gylq1#

必须通过服务器/应用程序/部分/设置层次结构,

using System;
using Microsoft.Web.Administration;

namespace CookieTimeout
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the server manager
            ServerManager serverManager = new ServerManager();

            // Get the app configuration
            Configuration config = serverManager.Sites["Default Web Site"].Applications["/App"].GetWebConfiguration();

            // Get the session state section
            ConfigurationSection sessionStateSection = config.GetSection("system.web/sessionState");

            // Set the cookie timeout to 20 minutes
            sessionStateSection["timeout"] = TimeSpan.FromMinutes(20);

            // Save the changes
            serverManager.CommitChanges();

            // Display a message
            Console.WriteLine("Cookie timeout set to 20 minutes);
        }
    }
}

相关问题