将 IAppBuilder 的属性与 IApplicationBuilder 一起使用

Using Properties of IAppBuilder with IApplicationBuilder

我正在尝试设置启动配置 (startup.cs),我想将 IAppBuilder 的一些属性与 IApplicationBuilder 一起使用。这是我的原始代码:

 public void ConfigureAuth(IAppBuilder app) {
  // Configure the db context, user manager and role manager to use a single instance per request
  app.CreatePerOwinContext(ApplicationDbContext.Create);
  app.CreatePerOwinContext < ApplicationUserManager > (ApplicationUserManager.Create);
  app.CreatePerOwinContext < ApplicationRoleManager > (ApplicationRoleManager.Create);
  app.CreatePerOwinContext < ApplicationSignInManager > (ApplicationSignInManager.Create);

  // 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
  // Configure the sign in cookie
  app.UseCookieAuthentication(new CookieAuthenticationOptions {
   AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider {
     // Enables the application to validate the security stamp when the user logs in.
     // This is a security feature which is used when you change a password or add an external login to your account.  
     OnValidateIdentity = SecurityStampValidator.OnValidateIdentity < ApplicationUserManager, ApplicationUser, string > (
      validateInterval: TimeSpan.FromMinutes(30),
      regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
      // Need to add THIS line because we added the third type argument (int) above:
      getUserIdCallback: (claim) => claim.GetUserId())
    }
  });
 }

我正在创建一个 Asp .NET Core Web 应用程序(.NET 框架)并希望将上述代码与 IApplicationBuilder 一起使用,但它没有这些属性。
这是新的启动结构:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
 //LOGIC
}

是否有任何替代方法可以实现与 IApplicationBuilder 相同的效果?
请帮忙谢谢。

你需要ConfigureServices方法(我假设你想使用身份),代码可能是这样的:

public void ConfigureServices(IServiceCollection services)
{
       // Add framework services.
     services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

     services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
     services.Configure<IdentityOptions>(options =>
     {
         options.Cookies.ApplicationCookie.LoginPath= = new PathString("/Account/Login"); 
         options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents
         {
             OnValidatePrincipal =  async (context) =>
             {
                  // validate user
                  if(not valid)
                  {
                       context.RejectPrincipal();
                       await context.HttpContext.Authentication.SignOutAsync();
                  }
               }
         }
    };
});

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{
    // ...  
    app.UseIdentity();
}