2014-07-21 33 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 =>这将显示给定会话的所有数据。该值以字节流的形式存储,因此人们不太可读,但键是字符串。