使用IAppBuilder的属性和IApplicationBuilder

时间:2016-08-15 11:07:09

标签: asp.net-mvc asp.net-core owin asp.net-core-mvc .net-core

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

 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核心Web应用程序(.NET框架),并希望将上述代码与IApplicationBuilder一起使用,但它没有这些属性。
这是新的启动结构:

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

是否有任何替代方法可以与IApplicationBuilder实现相同的目标? 请帮助谢谢。

1 个答案:

答案 0 :(得分:1)

你需要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();
}