我一直在尋找ASP.NET身份驗證附帶的ASP.NET Web API默認項目,它使用Owin。我已經搜索了一下,看到Owin是爲了將應用與服務器分離開來的,並且它的功能基於一個Startup
類,它真的會在項目中退出。然而,類被分成兩個文件,就是這樣Startup課程如何發揮作用?
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
public partial class Startup
{
static Startup()
{
PublicClientId = "self";
UserManagerFactory =() => new UserManager<IdentityUser>(new UserStore<IdentityUser>());
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
}
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static Func<UserManager<IdentityUser>> UserManagerFactory { get; set; }
public static string PublicClientId { get; private set; }
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication();
}
}
第一個文件,上面的名稱空間存在此行
[assembly: OwinStartup(typeof(WebApplication1.Startup))]
現在這個類是用來只是在auth控制器上,但基本上它僅用於構建用戶管理器並獲取OAuthOptions.AccessTokenFormat
和PublicClientId
信息。
在該設置中,該類僅用於提供這些信息。那麼,這個班級真的起作用了嗎?我真的不理解Owin和這個類之間的關係,它只是提供配置信息。