【问题标题】:MVC 5 customise OWIN authentication options in web.configMVC 5 在 web.config 中自定义 OWIN 身份验证选项
【发布时间】:2013-11-28 10:10:56
【问题描述】:

是否可以在 web.config 文件中指定一些选项?创建新项目时,默认情况下您会获得此启动类,并且旧的表单身份验证部分是 web.config 已消失。

    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    }

我希望能够在此处列出的 CookieAuthenticationOptions 上指定一些选项:

http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx#_Understanding_OWIN_Forms

在 web.config 中(例如过期超时)。

【问题讨论】:

    标签: asp.net-mvc authentication asp.net-mvc-5 owin


    【解决方案1】:

    将 OWin 相关属性放入 appSettings 的替代方法是编写自定义 ConfigurationSection。这个类可以包含所有必要的管道。另外,XML 模式可以添加代码完成。



    示例代码

    public static class IAppBuilderExtensions {
        public static void ApplyConfigSettings(this IAppBuilder appBuilder) {
            var config = (OWinConfigSection) System.Configuration.ConfigurationManager.GetSection("owinConfig");
            if (config == null) return;
            if (config.GoogleAuthentication.Enabled) appBuilder.UseGoogleAuthentication();
        }
    }
    
    public class OWinConfigSection : ConfigurationSection {
        [ConfigurationProperty("GoogleAuthentication", IsRequired=false)]
        public GoogleConfigurationElement GoogleAuthentication
        { get { return (GoogleConfigurationElement)this["GoogleAuthentication"]; } }
    }
    
    public class GoogleConfigurationElement : ConfigurationElement {
        [ConfigurationProperty("Enabled", DefaultValue = "false", IsRequired = false)]
        public bool Enabled
        { get { return (bool)this["Enabled"]; } set { this["Enabled"] = value; } }
    }
    

    将以下 XML sn-p 放入 configuration 元素的 configSections 内:

    <section name="owinConfig" type="OWinConfigSection" requirePermission="false" />
    

    然后,只需在 OWin 启动期间的某个地方调用 app.ApplyConfigSettings();

    示例架构

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="XMLSchema1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:complexType name="owinConfig_T">
        <xs:all minOccurs="0">
          <xs:element name="GoogleAuthentication" type="GoogleAuthentication_T" />
        </xs:all>
      </xs:complexType>
      <xs:complexType name="GoogleAuthentication_T">
        <xs:attribute name="Enabled" use="optional" default="false">
          <xs:annotation>
            <xs:documentation>Set to true to enable Authentication via Google OAuth</xs:documentation>
          </xs:annotation>
        </xs:attribute>
      </xs:complexType>
      <xs:element name="owinConfig" type="owinConfig_T" />
    </xs:schema>
    

    【讨论】:

      【解决方案2】:

      Cookie 中间件或任何 auth 中间件都没有任何相应的 web.config 设置来配置属性。它们只是代码。或者,您可以在 web.config 中有 appSettings 并在 ConfigureAuth 方法中分配这些 appSetting 值。

      <appSettings>
          <add key="ExpireTimeSpanInMinutes" value="10" />
        </appSettings>
      
      public void ConfigureAuth(IAppBuilder app)
          {
      var expireTimeSpan = TimeSpan.FromMinutes(Int32.Parse(ConfigurationManager.AppSettings["ExpireTimeSpanInMinutes"]));
      ....
      ..
      }
      

      【讨论】:

        猜你喜欢
        • 2014-01-02
        • 2014-11-15
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 2014-11-25
        • 2015-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多