简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM