簡體   English   中英

將IAppBuilder的屬性與IApplicationBuilder一起使用

[英]Using Properties of IAppBuilder with IApplicationBuilder

我想設置啟動配置(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 Core Web應用程序(.NET框架),並希望將以上代碼與IApplicationBuilder一起使用,但它沒有這些屬性。
這是新的啟動結構:

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

是否有其他替代方法可以使用IApplicationBuilder實現相同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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM