是否可以在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上指定一些选项:
web.config中的(例如过期超时)。
答案 0 :(得分:6)
在appSettings中放置与OWin相关的属性的另一种方法是编写自定义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" />
然后,只需在OWin Startup期间的某处调用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>
答案 1 :(得分: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"]));
....
..
}