How do you set the Session State – Cookie Settings – Timeout in an IIS web application using Microsoft.Web.Administration?

huangapple go评论50阅读模式
英文:

How do you set the Session State - Cookie Settings - Timeout in an IIS web application using Microsoft.Web.Administration?

问题

我正在使用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); // 这里应该填什么?或者是否需要使用不同的函数,如SetMetadata?


这是我想要在IIS UI中更改的值的图片,供参考:[![cookie settings timeout textbox](https://i.stack.imgur.com/DbZi8.png)](https://i.stack.imgur.com/DbZi8.png)

[Microsoft的开发者网站](https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.administration.application?view=iis-dotnet)上也没有清楚说明如何完成这个操作。

提前感谢您的任何帮助。
英文:

I am using Microsoft.Web.Administration to create IIS web applications under a selected parent site programmatically. I can create the application just fine with the code below but I'm not sure what to do to change this specific aspect of the application (the cookie timeout value) through the Application variable.

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?

Here is a picture of the value I'm trying to change in the IIS UI, for reference: How do you set the Session State – Cookie Settings – Timeout in an IIS web application using Microsoft.Web.Administration?

Microsoft's developer site here is not clear on how I would do this either.

Thanks in advance for any help.

答案1

得分: 1

你必须按照 server/app/section/setting 层次结构进行操作,

using System;
using Microsoft.Web.Administration;

namespace CookieTimeout
{
    class Program
    {
        static void Main(string[] args)
        {
            // 获取服务器管理器
            ServerManager serverManager = new ServerManager();

            // 获取应用程序配置
            Configuration config = serverManager.Sites["Default Web Site"].Applications["/App"].GetWebConfiguration();

            // 获取会话状态部分
            ConfigurationSection sessionStateSection = config.GetSection("system.web/sessionState");

            // 将Cookie超时设置为20分钟
            sessionStateSection["timeout"] = TimeSpan.FromMinutes(20);

            // 保存更改
            serverManager.CommitChanges();

            // 显示消息
            Console.WriteLine("Cookie超时设置为20分钟);
        }
    }
}
英文:

You must go through the server/app/section/setting hierarchy,

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);
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月14日 05:49:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定