8

我在尝试决定承担我的项目的路线时遇到问题。

我一直在阅读 OWIN 规范和 .NET 中的 Katana 实现。我想使用 Katana 路线的原因是因为与 ADFS 和 Token/Cookie 生成相关的 owin 组件。

我有两个项目,一个用于 MVC 5 网站,一个用于 Web API。将来它们可能位于两台不同的服务器上,但现在它们在同一台服务器上。

我知道我将使用 IIS,因此我不需要调查 Owin 管道。

我的要求是,有些用户将使用 ADFS 登录,而其他用户将使用 Token/Cookie 生成以及角色/成员资格提供程序登录。根据谁通过身份验证,我的网页的某些部分将被公开。网页工程师是在剃刀中完成的。

有没有人可以阅读任何材料来帮助解释我可以采用的设计流程?或者有人做过类似于我正在经历的项目,可以添加任何建议?有很多不同的文档描述了我需要的具体事物,但不是全局;就像只谈论 WebAPI 和 ADFS,或者 WebAPI 和 windows azure 等。

我的理论是在MVC5网站项目上实现认证/授权,在Web API上进行授权(不知何故两者之间需要存在通信)。然后我可能会为 ADFS 创建一个项目副本并为令牌/cookie 身份验证创建另一个副本?或者也许我必须进行 4 种不同类型的身份验证:2 种用于我对 MVC5 网站和 Web API 进行身份验证的 adfs,另外 2 种用于令牌/cookie 生成。

任何建议都会有所帮助,因为我对这种技术不是很熟悉。

4

2 回答 2

10

我可以提供 OWIN 中的 WsFederation 选项很好,但需要 cookie……而且它们是与带有 cookie 的本地身份验证不同类型的 cookie。ADFS 2.0/WsFederation 使用 AuthenticationType="Cookies",本地身份验证使用 AuthenticationType="ApplicationCookie"。据我所知,它们显然是不相容的。我认为您必须对 ADFS 使用令牌身份验证,但我认为这需要 2012R2 上的 ADFS 3.0。为此,请使用 OWIN OAuth。

更新:在研究了一段时间后,我想出了如何让这两种身份验证类型在同一个 Web 应用程序中和平共存。使用 OWIN,设置两次调用 UseCookieAuthentication,一次启用新的 WsFederationAuthentication 中间件,再次启用本地 cookie 身份验证。这并不直观,但在幕后,为每个指定不同的身份验证类型将它们设置为不同的身份验证“引擎”。这是它在我的 Startup 中的外观:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
        {
            OnResponseSignIn = ctx =>
            {
                ctx.Identity = TransformClaims(ctx, app);
            }
        }
});

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    Provider = new CookieAuthenticationProvider
    {
        OnResponseSignIn = ctx =>
        {
            ctx.Identity = TransformClaims(ctx, app);
        }
    }
});

app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
{
    Wtrealm = Realm,
    MetadataAddress = Metadata,
    Caption = "Active Directory",
    SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType
});

这成功地允许用户对本地 SQL 表或 ADFS 2.0 进行身份验证。TransformClaims 标注允许我规范这两个提供者之间的声明,以便它们保持一致。

编辑:这是一个非常基本的 TransformClaims。你可以在其中做很多事情:从你的数据库中获取用户,设置导航声明,自定义权限,角色集合等等。我刚刚从一个更大的实现中构建了这个精简版本,所以我没有运行它,但希望您了解如何利用 OnResponseSignIn 事件。

private static ClaimsIdentity TransformClaims(CookieResponseSignInContext ctx, IAppBuilder app)
{
    var ident = ctx.Identity;

    var claimEmail = ident.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Email);
    var claimName = ident.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Name);

    //normalize my string identifier
    var loginString = (claimEmail != null) ? claimEmail.Value : (claimName != null) ? claimName.Value : null;
    var efctx = ctx.OwinContext.Get<DBEntities>();

    var user = UserBL.GetActiveUserByEmailOrName(efctx, loginString);
    if (user == null)
    {
        //user was auth'd by ADFS but hasn't been auth'd by this app
        ident.AddClaim(new Claim(ClaimTypesCustom.Unauthorized, "true"));
        return ident;
    }

    if (ident.Claims.First().Issuer == "LOCAL AUTHORITY")
    {
        //Local
        //local already has claim type "Name"
        //local didn't have claim type "Email" - adding it
        ident.AddClaim(new Claim(ClaimTypes.Email, user.Email));
    }
    else
    {
        //ADFS
        //ADFS already has claim type "Email"
        //ADFS didn't have claim type "Name" - adding it
        ident.SetClaim(ClaimTypes.Name, user.UserName);
    }

    //now ident has "Name" and "Email", regardless of where it came from
    return ident;
}
于 2014-05-07T15:46:51.370 回答
1

Cmedine,根据布雷特的回答,我配置了我的身份验证和授权。当您请求一些示例代码时,我会向您展示代码。

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            SlidingExpiration = false,
            CookieName = "identity",
            //short time for testing only
            ExpireTimeSpan = TimeSpan.FromSeconds(120),
            Provider = new CookieAuthenticationProvider
            {
                OnResponseSignIn = ctx =>
                {
                    ctx.Identity = TransformClaims(ctx);
                }
            }
        });

        app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                MetadataAddress = "https://[[ADFS_Url]]/FederationMetadata/2007-06/federationmetadata.xml",
                Wtrealm = "[[realm]]",
                UseTokenLifetime = false
            }
        );

    }

    private ClaimsIdentity TransformClaims(CookieResponseSignInContext ctx)
    {
        return new IdentityCreator().CreateIdentity(ctx.Identity, [[ApplicationName]]);
    }
}

IdentityCreator 获取 ClaimsIdentity 和应用程序名称,然后转到数据库并获取该应用程序中该用户的声明。希望能帮助到你!!

于 2015-04-10T16:02:08.197 回答