Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Forms Authentication Without Web.Config

Scenario


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)

Question


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()

like image 741
Dwayne Hinterlang Avatar asked Oct 21 '22 00:10

Dwayne Hinterlang


1 Answers

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);
    }
}
like image 166
Dennis Avatar answered Oct 22 '22 16:10

Dennis