gpt4 book ai didi

c# - 在没有 Web.Config 的情况下配置表单例份验证

转载 作者:太空狗 更新时间:2023-10-29 23:30:53 24 4
gpt4 key购买 nike

场景


我已将我的 MVC 应用程序配置为以传统方式使用 Forms 身份验证。

<authentication mode="Forms">
<forms cookieless="UseCookies" slidingExpiration="true" timeout="1">
</forms>
</authentication>

但是,我允许客户端选择他的 Web 应用程序如何进行身份验证(URL Token/Cookie),以及他的应用程序 session 在到期前应持续多长时间(超时)

问题


我有办法通过代码来做到这一点吗?我只通过 web.config 看到过这个实现吗?

我想从数据库中读取设置并将它们应用到 Global.asax -> OnApplicationStart()

最佳答案

看看WebActivatorEx .这里有个好blog关于它的能力。要配置 FormsAuthentication,您甚至必须在应用程序启动之前执行您的配置方法。 WebActivatorEx 将为您处理。如果不想使用第三方包,可以在 AssemblyInfo.cs 类中指定此[assembly: PreApplicationStartMethod(typeof(AppConfig), "Configure")]Here's another point of reference that talks about it.

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(AppConfig), "Configure")]
public static class AppConfig
{
public static void Configure()
{
var settings = new NameValueCollection();
settings.Add("defaultUrl", "~/Account/Default.aspx");
settings.Add("loginUrl", "~/Default.aspx");
settings.Add("timeout", "10");
FormsAuthentication.EnableFormsAuthentication(settings);
}
}

关于c# - 在没有 Web.Config 的情况下配置表单例份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com