【发布时间】:2014-04-25 01:27:48
【问题描述】:
我一直在研究 ASP.NET Web API 默认项目,它带有 ASP.NET 身份验证,它使用 Owin。我搜索了一下,发现 Owin 旨在将应用程序与服务器分离,并且它的功能基于 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))]
现在这个类仅用于身份验证控制器,但基本上它仅用于构建用户管理器并获取OAuthOptions.AccessTokenFormat 和PublicClientId 信息。
在该设置中,该类仅用于提供这些信息。那么,这个类是如何真正发挥作用的呢?我真的不明白 Owin 和这个显然只是提供配置信息的类之间的关系。
【问题讨论】:
标签: c# asp.net asp.net-web-api owin