use*_*696 3 c# asp.net asp.net-web-api owin
我一直在看ASP.NET API 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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,名称空间上方的第一个文件具有此行
[assembly: OwinStartup(typeof(WebApplication1.Startup))]
Run Code Online (Sandbox Code Playgroud)
现在这个类只在auth控制器上使用,但基本上它只用于构建用户管理器并获取OAuthOptions.AccessTokenFormat和PublicClientId信息.
在该设置中,该类仅用于提供这些信息.那么,这堂课真正发挥作用呢?我真的不理解Owin和这个类之间的关系,它通常只提供配置信息.
您需要将组件添加到Owin应用程序管道中以处理HTTP请求,否则不会发生任何事情:(
Owin使用约定优于配置的软件设计原则来假设您的项目具有一个名为的类Startup,该类具有一个名为的类,该类Configuration接受该类型的一个参数,该参数IAppBuilder将组件添加到Owin应用程序管道中.这背后的目的是让程序员更快更轻松地设置内容,这样您就可以花更多的时间来创建API.
如果由于任何原因你无法完全遵循Startup类约定,那么有其他方法可以将Owin指向将组件添加到Owin应用程序管道中的代码.您已经确定了其中一种方法:OwinStartup属性.
[assembly: OwinStartup(typeof(WebApplication1.Startup))]
Run Code Online (Sandbox Code Playgroud)
您可以了解更多关于什么OWIN启动类不和它是如何检测到这里.
| 归档时间: |
|
| 查看次数: |
8873 次 |
| 最近记录: |