身份提供器

2017-02-25  本文已影响295人  灭蒙鸟

layout: docs-default

身份提供器

IdentityServer支持通过第三方身份认证器来认证,第三方认证机制必须封装在katana认证中间件中。

katana本身带有Google,Facebook,Twitter,Microsoft Accounts,WS-Federation和OpenID Connect认证中间件。社区也提供了一些其它认证中间件(如 Yahoo, LinkedIn, and SAML2p). 请看 完整列表.

要支持第三方登陆,项目里需要增加一个带IAppBuilder 和 a string 参数的方法。

public static void ConfigureIdentityProviders(IAppBuilder app, string signInAsType)
{
    var google = new GoogleOAuth2AuthenticationOptions
    {
        AuthenticationType = "Google",
        Caption = "Google",
        SignInAsAuthenticationType = signInAsType,
        ClientId = "...",
        ClientSecret = "..."
    };
    app.UseGoogleAuthentication(google);

    var fb = new FacebookAuthenticationOptions
    {
        AuthenticationType = "Facebook",
        Caption = "Facebook",
        SignInAsAuthenticationType = signInAsType,
        AppId = "...",
        AppSecret = "..."
    };
    app.UseFacebookAuthentication(fb);

    var twitter = new TwitterAuthenticationOptions
    {
        AuthenticationType = "Twitter",
        Caption = "Twitter",
        SignInAsAuthenticationType = signInAsType,
        ConsumerKey = "...",
        ConsumerSecret = "..."
    };
    app.UseTwitterAuthentication(twitter);
}

解释

必须把这个方法传递给AuthenticationOptionsIdentityProviders属性里。

var idsrvOptions = new IdentityServerOptions
{
    SiteName = "IdentityServer3",
    Factory = factory,
    SigningCertificate = Cert.Load(),

    AuthenticationOptions = new AuthenticationOptions 
    {
        IdentityProviders = ConfigureIdentityProviders
    }
};

app.UseIdentityServer(idsrvOptions);

添加WS-Federation 第三方登陆

基于WS-Federation的第三方登陆可以和上面一样的方式配置。

出于向后兼容的原因,WS-Federation中间件会监听所有的请求并检查她们带有的token数据。如果有多于一个WS-Federation中间件,就需要显示设置一个唯一的CallbackPath属性,这个属性需要匹配IdP里的返回URL的。注意CallbackPath必须是相对于根目录,不是相对于Identity Server模块的目录。比如:第三方登陆配置post auth tokens 到 http://mydomain.com/SubFolder/IdSrv/MyExternalProvider, 然后 CallbackPath 应该配置为 /SubFolder/IdSrv/MyExternalProvider.

var adfs = new WsFederationAuthenticationOptions
{
    AuthenticationType = "adfs",
    Caption = "ADFS",
    SignInAsAuthenticationType = signInAsType,

    MetadataAddress = "https://adfs.leastprivilege.vm/federationmetadata/2007-06/federationmetadata.xml",
    Wtrealm = "urn:idsrv3"
};
app.UseWsFederationAuthentication(adfs);
上一篇 下一篇

猜你喜欢

热点阅读