2014-07-21 94 views
3

我有一個Azure PaaS,我想爲高可用性進行配置,並且我已向該實例添加了另一個角色,現在我需要配置ASP.Net會話以存儲在分佈式緩存中。我發現以下信息,關於如何使用緩存:使用Azure redis.cache跨負載均衡服務存儲MVC應用程序會話

http://azure.microsoft.com/en-us/documentation/articles/cache-dotnet-how-to-use-azure-redis-cache/#store-session

我已登錄到Azure和創建預覽緩存。我加入了StackExchange.Redis NuGet包和RedisSessionStateProvider的NuGet和我的web配置現在看起來如下:

<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> 
    <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />  
    </configSections> 

    <connectionStrings> 
    <add name="MetaLearningContext" connectionString="Data Source=server.database.windows.net;Initial Catalog=databasename;User [email protected];Password=password;" providerName="System.Data.SqlClient" />  
    </connectionStrings> 

    <appSettings> 
    ................. 
    </appSettings> 

    <location path="FederationMetadata"> 
    <system.web> 
     <authorization> 
     <allow users="*" /> 
     </authorization>  
     <sessionState mode="Custom" customProvider="MySessionStateStore"> 
     <providers> 
      <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="metalearningdev.redis.cache.windows.net" port="6380" accessKey="accesskeyhere" ssl="true" /> 
     </providers> 
     </sessionState> 
    </system.web> 
    </location> 

在那裏我得到了登錄用戶的用戶名我叫下面的方法有兩個獲得和方法兩組的緩存,但看着門戶網站日誌,我不能看到對方的任何來電:

public static void GetUserName() 
    { 
     ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("metalearningdev.redis.cache.windows.net,ssl=true,password=passwrod"); 

     // connection referes to a previously configured ConnectionMultiplexer 
     IDatabase cache = connection.GetDatabase(); 

     // Perform cache operations using the cache object... 
     // Simple put of integral data types into the cache 
     cache.StringSet("key1", "value"); 
     cache.StringSet("key2", 25); 

     // Simple get of data types from the cache 
     string key1 = cache.StringGet("key1"); 
     int key2 = (int)cache.StringGet("key2"); 

     string userName = ""; 
     string domainStub = ""; 
     bool updatedLogin = false; 
     string loginTime = ""; 
     //if (System.Configuration.ConfigurationManager.AppSettings["authenticationType"].ToString() == "ADFS")    
     if (System.Configuration.ConfigurationManager.AppSettings["platformType"].ToString() == "Cloud")  
     { 
      //string userName = ""; 
      System.Security.Claims.ClaimsIdentity claimsIdentity = (System.Security.Claims.ClaimsIdentity)System.Threading.Thread.CurrentPrincipal.Identity; 

      foreach (System.Security.Claims.Claim claim in claimsIdentity.Claims) 
      { 
       if (claim.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/windowsaccountname") 
       { 
        userName = claim.Value; 
       } 
       else if(claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name") 
       { 
        userName = HttpContext.Current.User.Identity.Name; 
        domainStub = "FORMS"; 
       } 
       else if (claim.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationinstant") 
       { 
        loginTime = claim.Value; 
        updatedLogin = true; 
       } 
      } 

      if (userName.Contains("\\")) 
      { 
      string[] stringArray = userName.Split(new Char[] { '\\' }); 
      domainStub = stringArray[0]; 
      userName = stringArray[1]; 
      }    
      HttpContext.Current.Session["domainStub"] = domainStub; 
      HttpContext.Current.Session["userName"] = userName; 
      HttpContext.Current.Session["updatedLogin"] = updatedLogin; 
      HttpContext.Current.Session["loginTime"] = loginTime; 
      HttpContext.Current.Session["companyName"] = System.Configuration.ConfigurationManager.AppSettings["companyName"].ToString(); 
      //HttpContext.Current.Session["companyName"] = System.Configuration.ConfigurationManager.AppSettings["companyName"].ToString(); 
     }    
     else if (System.Configuration.ConfigurationManager.AppSettings["platformType"].ToString() == "internal") 
     { 
      userName = HttpContext.Current.Request.ServerVariables["AUTH_USER"]; 
      if (userName.Contains("\\")) 
      { 
       string[] stringArray = userName.Split(new Char[] { '\\' }); 
       domainStub = stringArray[0]; 
       userName = stringArray[1]; 
      } 
      HttpContext.Current.Session["domainStub"] = domainStub; 
      HttpContext.Current.Session["userName"] = userName; 
      HttpContext.Current.Session["companyName"] = System.Configuration.ConfigurationManager.AppSettings["companyName"].ToString(); 
     } 
    } 

有人能看到什麼,我爲了得到保存到緩存中的會話做錯了什麼?

+0

上面的代碼和預期一樣我只是wasnt看到任何GET/SET計數會話不確定這些是在幕後處理還是其他任何事情,但會話跨多個實例保存。 – Jay

回答

1

可以使用Redis的的CLI(命令行界面),並連接到遠程的Redis服務器:

Connecting to remote redis server

然後你就可以「GET」將密鑰的值並檢查它是否被設置好的。

您可以在這裏下載的CLI窗口:

https://github.com/MSOpenTech/redis

用於Linux/MAC OS可以使用apt-get/wget的

+0

或者只是使用普通的舊telnet連接到您的Redis端口 –

1

蒂亞戈庫斯托迪奧是正確的。除此之外:

會話以散列形式存儲在redis中。

  1. KEYS * =>這會給你所有可用的會話。所有會話將具有名稱,如__Data
  2. HGETALL __Data =>這將顯示給定會話的所有數據。該值以字節流的形式存儲,因此人們不太可讀,但鍵是字符串。