在项目外部配置IAppBuilder

本文关键字:IAppBuilder 配置 外部 项目 | 更新日期: 2023-09-27 18:01:00

我正在尝试创建一个方法,该方法将在不同的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总是红色的,visual studio无法识别,因此无法构建它。我似乎无法弄清楚缺少了什么引用,因为我在Startup中使用的语句都一样。VS也没有提出任何建议。

我缺少什么参考资料来完成这项工作?

在项目外部配置IAppBuilder

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

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

DefaultAuthenticationTypes类