我有一个Azure PaaS,我想为高可用性进行配置,并且我已向该实例添加了另一个角色,现在我需要配置ASP.Net会话以存储在分布式缓存中。我发现以下信息,关于如何使用缓存:使用Azure redis.cache跨负载均衡服务存储MVC应用程序会话
我已登录到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();
}
}
有人能看到什么,我为了得到保存到缓存中的会话做错了什么?
上面的代码和预期一样我只是wasnt看到任何GET/SET计数会话不确定这些是在幕后处理还是其他任何事情,但会话跨多个实例保存。 – Jay