MVC 5在web.config中自定义OWIN身份验证选项

用户名

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

斯蒂芬·汉克

将OWin相关属性放入appSettings中的一种替代方法是编写一个custom 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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在ASP.NET MVC5中使用自定义登录进行表单身份验证

来自分类Dev

在ASP.NET MVC5中使用自定义登录进行表单身份验证

来自分类Dev

在基于OWIN的MVC 5中为多个Web应用设置表单身份验证

来自分类Dev

使用MVC5和OWIN的自定义身份

来自分类Dev

使用MVC5和OWIN的自定义身份

来自分类Dev

Asp.net 5 MVC 6自定义身份验证

来自分类Dev

Asp.net 5 MVC 6自定义身份验证

来自分类Dev

.NET MVC中Elmah的自定义身份验证

来自分类Dev

身份验证/授权MVC 5和Web API-Katana / Owin

来自分类Dev

MVC 5和Web API之间的Owin身份验证(单独的IIS应用程序)

来自分类Dev

MVC 5中的身份验证

来自分类Dev

带有OWIN的ASP.NET Web Api-自定义身份验证

来自分类Dev

在现有的MVC 5应用中实施OWIN身份验证

来自分类Dev

MVC5中的自定义验证错误消息

来自分类Dev

Elmah.Mvc 2.0自定义身份验证

来自分类Dev

使用MVC进行自定义身份验证

来自分类Dev

使用Windows身份验证和OWIN的ASP.NET MVC5 / AngularJS / Web API应用

来自分类Dev

tumblr像mvc3中的自定义域名跨域身份验证

来自分类Dev

未在MVC 5中使用Owin在浏览器中设置身份验证cookie

来自分类Dev

在IdentityVC中将被动身份验证与MVC5中的OWIN一起使用

来自分类Dev

MVC 5 OWIN身份验证(Google范围)/不带EF的身份

来自分类Dev

MVC 5 OWIN身份验证(Google范围)/不带EF的身份

来自分类Dev

Web API 2自定义身份验证

来自分类Dev

Laravel 5 自定义身份验证尝试

来自分类Dev

Web API 令牌身份验证与自定义令牌身份验证

来自分类Dev

MVC 5中的身份验证筛选器

来自分类Dev

如何最好地覆盖MVC 5中的身份验证?

来自分类Dev

MVC 5,Web API和Owin

来自分类Dev

MVC Web Api身份验证失败

Related 相关文章

热门标签

归档