【问题标题】:How does the Startup class come into play?Startup 课程如何发挥作用?
【发布时间】: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.AccessTokenFormatPublicClientId 信息。

在该设置中,该类仅用于提供这些信息。那么,这个类是如何真正发挥作用的呢?我真的不明白 Owin 和这个显然只是提供配置信息的类之间的关系。

【问题讨论】:

    标签: c# asp.net asp.net-web-api owin


    【解决方案1】:

    您需要将组件添加到 Owin 应用程序管道中以处理 HTTP 请求,否则将不会发生任何事情:(

    Owin 使用Convention over configuration 的软件设计原则假设您的项目有一个名为Startup 的类,以及一个名为Configuration 的方法,该方法接受一个IAppBuilder 类型的参数,它将组件添加到Owin 应用程序管道。这样做的目的是让您(程序员)更快、更轻松地进行设置,以便您可以花更多时间创建 API。

    如果由于任何原因您不能完全遵循 Startup 类约定,则可以使用其他方法将 Owin 指向将组件添加到 Owin 应用程序管道中的代码。您已经确定了其中一种方法:OwinStartup 属性。

    [assembly: OwinStartup(typeof(WebApplication1.Startup))]
    

    您可以阅读更多关于 OWIN 启动类的作用以及如何检测到它的信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-17
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多