I've configured my MVC application to use Forms authentication in the traditional fashion.
<authentication mode="Forms">
<forms cookieless="UseCookies" slidingExpiration="true" timeout="1">
</forms>
</authentication>
However, I allow the client to choose how his web application authenticates (URL Token / Cookie), as well as how long his application session should last before expiring (Timeout)
Is there a way for me to do this via code? I've only seen implementations of this via web.config?
I'd like to read the settings from the database and apply them in Global.asax -> OnApplicationStart()
Take a look at WebActivatorEx. Here's a good blog about its capabilities. To configure FormsAuthentication, you'd have to execute your configuration method even before the application starts. WebActivatorEx will handle that for you. You could specify this [assembly: PreApplicationStartMethod(typeof(AppConfig), "Configure")] in the AssemblyInfo.cs class if you don't want to use third party packages. 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With