在项目之外配置IAppBuilder

时间:2016-08-05 21:00:19

标签: c# asp.net-mvc-5 asp.net-identity

我正在尝试创建一个在不同的dll中配置IAppBuilder的方法。我正在尝试使用Identity和Owin,我只是想了解事情是如何运作的。

以下代码有效:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserService, User>(
                    TimeSpan.FromMinutes(30), (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

        app.UseTwitterAuthentication(
           consumerKey: "",
           consumerSecret: ""
           );

        app.UseFacebookAuthentication(
           appId: "",
           appSecret: ""
        );

        app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "",
            ClientSecret = ""
        });

        app.UseSteamAuthentication("");
    }
}

我想做的是:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app = new AppBuilderService.BuildApp(app);
    }
}

我尝试添加到BuildApp方法的代码:

public class AppBuilderService
{
    public IAppBuilder BuildApp(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserService, User>(
                    TimeSpan.FromMinutes(30), (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

        app.UseTwitterAuthentication(
           consumerKey: "",
           consumerSecret: ""
           );

        app.UseFacebookAuthentication(
           appId: "",
           appSecret: ""
        );

        app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "",
            ClientSecret = ""
        });

        app.UseSteamAuthentication("");
    }

    return app;
}

正如您所看到的,代码几乎是完全相同的。我遇到的问题是CookieAuthenticationOptions.AuthenticationType总是红色,视觉工作室无法识别,因此无法构建它。我似乎无法弄清楚缺少什么参考,因为我在Startup中使用了所有相同的语句。 VS也没有提出任何建议。

我缺少什么参考来使这项工作?

1 个答案:

答案 0 :(得分:0)

DefaultAuthenticationTypesMicrosoft.AspNet.Identity

Assembly Microsoft.AspNet.Identity.Core.dll, v2.0.0.0的一部分

OWIN主要使用它来枚举与Microsoft ASP.NET Identity 2.0相关联的默认身份验证类型

DefaultAuthenticationTypes Class