2016-08-05 57 views
1

我正在嘗試創建一個方法,它將在另一個dll中配置IAppBuilder。我正在試驗Identity和Owin,我只是想了解事情是如何運作的。在項目之外配置IAppBuilder

下面的代碼工作:

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也沒有提出任何建議。

我錯過了什麼參考使這項工作?

+0

'DefaultAuthenticationTypes'是一部分默認的身份驗證類型Microsoft.AspNet.Identity一部分'Assembly.microsoft.AspNet.Identity.Core.dll,v2.0.0.0'中找到'Microsoft.AspNet.Identity' – Nkosi

+0

@Nkosi看到我在Owin一直在搜索它https://msdn.microsoft.com/en-us /library/microsoft.owin.security.authenticationoptions (V = vs.113)的.aspx – Bojan

回答

0

DefaultAuthenticationTypesAssembly Microsoft.AspNet.Identity.Core.dll, v2.0.0.0

發現它主要由OWIN枚舉與Microsoft ASP.NET身份相關聯的2.0

DefaultAuthenticationTypes Class