2016-04-07 28 views
4

私のプロジェクトでは、いくつかのものを保存するためにredisキャッシュを使用しています。AzureのASP.NET Coreを使用したRedisでのユーザーセッションの保存

私はアズール(WebAppの)を使用しています、と私は生産に私の試作環境の間でSWAPを行う際に、ユーザセッションが失われ、彼は私のWebページに再ログインする必要があります。

私はUseCookieAuthenticationで、アイデンティティ3.0を使用しています。私はスワップを行う際に私の問題を解決するためにRedisに "セッション"を保存したいと思います。

私はそれについての情報やアイデアは見つかりませんでしたか?おかげ

Startup.csコードConfigureServices:

public void ConfigureServices(IServiceCollection services) 
     { 

         // Add framework services. 
      services.AddApplicationInsightsTelemetry(Configuration); 

      // Registers MongoDB conventions for ignoring default and blank fields 
      // NOTE: if you have registered default conventions elsewhere, probably don't need to do this 
      //RegisterClassMap<ApplicationUser, IdentityRole, ObjectId>.Init(); 

      AutoMapperWebConfiguration.Configure(); 

      services.AddSingleton<ApplicationDbContext>(); 

      // Add Mongo Identity services to the services container. 
      services.AddIdentity<ApplicationUser, IdentityRole>(o => 
      { 
       // configure identity options 
       o.Password.RequireDigit = false; 
       o.Password.RequireLowercase = false; 
       o.Password.RequireUppercase = false; 
       o.Password.RequireNonLetterOrDigit = false; 
       o.Password.RequiredLength = 6; 
       o.User.RequireUniqueEmail = true; 
       o.Cookies.ApplicationCookie.CookieSecure = CookieSecureOption.SameAsRequest; 
       o.Cookies.ApplicationCookie.CookieName = "MyCookie"; 
      }) 
       .AddMongoStores<ApplicationDbContext, ApplicationUser, IdentityRole>() 
       .AddDefaultTokenProviders(); 

      services.AddSession(options => 
      { 
       options.IdleTimeout = TimeSpan.FromMinutes(60); 
       options.CookieName = "MyCookie"; 
      }); 

      services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); 

      services.AddLocalization(options => options.ResourcesPath = "Resources"); 

      // Caching This will add the Redis implementation of IDistributedCache 
      services.AddRedisCache(); 

      services.Configure<RedisCacheOptions>(options => 
      { 
       options.Configuration = Configuration["RedisConnection"]; 
      }); 




      services.AddCaching(); 

      // Add MVC services to the services container. 
      services.AddMvc(options => 
      { 
       options.CacheProfiles.Add("OneDay", 
        new CacheProfile() 
        { 
         Duration = 86400, 
         Location = ResponseCacheLocation.Any 
        }); 

       options.CacheProfiles.Add("OneMinute", 
        new CacheProfile() 
        { 
         Duration = 60, 
         Location = ResponseCacheLocation.Any 
        }); 

      }) 
       .AddViewLocalization(options => options.ResourcesPath = "Resources") 
       .AddDataAnnotationsLocalization(); 



      services.Configure<AppOptions>(Configuration.GetSection("AppOptions")); 



     } 

Startup.csコード

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      // 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      app.UseApplicationInsightsRequestTelemetry(); 

      if (env.IsDevelopment()) 
      { 
       app.UseBrowserLink(); 
       app.UseDeveloperExceptionPage(); 
       app.UseDatabaseErrorPage(); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 

      } 

      app.UseSession(); 

      app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear()); 

      app.UseApplicationInsightsExceptionTelemetry(); 

      app.UseStaticFiles(); 

      app.UseIdentity(); 


      app.UseCookieAuthentication(options => 
      { 
       options.AutomaticAuthenticate = true; 
       options.LoginPath = new PathString("/Account/Login"); 
       options.AutomaticChallenge = true; 
      }); 

      var requestLocalizationOptions = new RequestLocalizationOptions 
      { 
       // Set options here to change middleware behavior 
       SupportedCultures = new List<CultureInfo> 
       { 
        new CultureInfo("en-US"), 
        new CultureInfo("es-ES") 
       }, 
       SupportedUICultures = new List<CultureInfo> 
       { 
        new CultureInfo("en-US"), 
        new CultureInfo("es-ES") 

       }, 
       RequestCultureProviders = new List<IRequestCultureProvider> 
       { 
        new CookieRequestCultureProvider 
        { 
         CookieName = "_cultureLocalization" 
        }, 
        new QueryStringRequestCultureProvider(), 
        new AcceptLanguageHeaderRequestCultureProvider 
        { 

        } 

       } 
      }; 

      app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture: new RequestCulture("en-US")); 

      app.UseFacebookAuthentication(options => 
      { 
       options.AppId = "*****"; 
       options.AppSecret = "****"; 
      }); 

      app.UseGoogleAuthentication(options => 
      { 
       options.ClientId = "*****"; 
       options.ClientSecret = "***"; 
      }); 



      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 

       routes.MapRoute(
        name: "view", 
        template: "{customName}/{id}", 
        defaults: new { controller = "View", action = "Index" }); 

      }); 

     } 
+0

Azure redisキャッシュを使用していますか? – juvchan

+0

はい、redisキャッシュを使用しています – chemitaxis

答えて

2

セッションが認証にリンクされていない、あなたは間違ってそれを解決しようとしています方法。

すべてのフォーム認証チケットとクッキーは暗号化され、データ保護層を使用して署名されています。遭遇している問題は、暗号化キーが保存されず、アプリケーションが互いに分離されているためです。あなたが暗号化キーの両方を共有し、あなたのコード内でアプリケーション名を設定する必要があり、それを解決するために

。すべての正直で、私はあなたがそうしないことをお勧めします。プリプロダクションはライブサービスではないため、両方を同時に認証することはできません。

あなたはこれを行う必要がありますように、あなたは、暗号鍵リングを共有し、そして固定されたアプリケーション名を設定する必要があると感じた場合。共有フォルダを使用してキーを共有することも、共有フォルダ(SQLやAzureストレージなど)に格納することもできます。そうするには、IXmlRepositoryを実装して、独自のキーリングプロバイダを作成する必要があります。キーを共有したら、データ保護の設定中にSetApplicationNameを使用して固定アプリケーション識別子を設定できます。

+0

ありがとうございますが、私は環境間でセッションを共有したくありません。私の問題は、私がスワップを行うときに、ユーザーがプロダクションでセッションを失ったことです。 – chemitaxis

+0

あなたの質問にはログインが記載されており、再度ログインする必要があります。だから、再ログインは問題ですか?またはセッション?または両方? – blowdart

+0

スワップ後にプロダクションで再ログインする必要がある – chemitaxis

関連する問題