2

是否可以在 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 中(例如到期超时)。

4

2 回答 2

6

将 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 片段放入元素configSections的 中:configuration

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

然后,只需app.ApplyConfigSettings();在 OWin 启动期间调用某处。

示例架构

<?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>
于 2013-11-17T11:54:16.413 回答
1

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"]));
....
..
}
于 2013-11-15T21:36:48.540 回答