olddev
olddev

Reputation: 11

UnableToConnect Azure Cache for Redis

I am trying to store ASP.NET session state in the cache (Azure cache for redis) as mentioned here https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-aspnet-session-state-provider

but, i am receiving below error.

UnableToConnect on mydomain.redis.cache.windows.net:6379/Interactive, origin: ResetNonConnected, input-buffer: 0, outstanding: 0, last-read: 5s ago, last-write: 5s ago, unanswered-write: 1106595s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 5s ago, mgr: Inactive, err: never StackExchange.Redis.RedisConnectionException: UnableToConnect on mydomain.redis.cache.windows.net:6379/Interactive, origin: ResetNonConnected, input-buffer: 0, outstanding: 0, last-read: 5s ago, last-write: 5s ago, unanswered-write: 1106595s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 5s ago, mgr: Inactive, err: never

    <add name="AzureRedisCacheConnection" connectionString="mydomain.redis.cache.windows.net:6379,password=password=,ssl=False,abortConnect=False"/>
  </connectionStrings>
  
  
  <sessionState mode="Custom" customProvider="AzureCacheForRedisProvider">
      <providers>
        <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki -->
        <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
        <!-- 'throwOnError','retryTimeoutInMilliseconds','databaseId' and 'applicationName' can be used with both options. -->
        <!--
          <add name="AzureCacheForRedisProvider" 
            host = "127.0.0.1" [String]
            port = "" [number]
            accessKey = "" [String]
            ssl = "false" [true|false]
            throwOnError = "true" [true|false]
            retryTimeoutInMilliseconds = "5000" [number]
            databaseId = "0" [number]
            applicationName = "" [String]
            connectionTimeoutInMilliseconds = "5000" [number]
            operationTimeoutInMilliseconds = "1000" [number]
            connectionString = "<Valid StackExchange.Redis connection string>" [String]
            settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String]
            settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String]
            loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
            loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
            redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String]
          />
        -->
        <add name="AzureCacheForRedisProvider" type="Microsoft.Web.Redis.RedisSessionStateProvider"
                     connectionString="AzureRedisCacheConnection" 
           
             />
      </providers>
    </sessionState> ````



What am I missing here?

Upvotes: 1

Views: 3354

Answers (1)

Jason Pan
Jason Pan

Reputation: 21883

I have read the offical doc, and it works for me.

If you want support 6379, you need to set Allow access only via SSL=No. enter image description here

I also find this offical blogs, I think it useful to you.

Announcing ASP.NET Session State Provider for Redis Preview Release

The content in my web.config file.

<sessionState mode="Custom" customProvider="MySessionStateStore">
  <providers>
    <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki -->
    <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
    <!-- 'throwOnError','retryTimeoutInMilliseconds','databaseId' and 'applicationName' can be used with both options. -->
    <!--
      <add name="MySessionStateStore" 
        host = "127.0.0.1" [String]
        port = "" [number]
        accessKey = "" [String]
        ssl = "false" [true|false]
        throwOnError = "true" [true|false]
        retryTimeoutInMilliseconds = "5000" [number]
        databaseId = "0" [number]
        applicationName = "" [String]
        connectionTimeoutInMilliseconds = "5000" [number]
        operationTimeoutInMilliseconds = "1000" [number]
        connectionString = "<Valid StackExchange.Redis connection string>" [String]
        settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String]
        settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String]
        loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
        loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
        redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String]
      />
    -->
    <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="jasonp2rediscache.redis.cache.windows.net:6380" accessKey="kKtOl***kLPg=" ssl="true" />
  </providers>
</sessionState>

My test step.

Step 1. Add Session["loginTime"] in login method.

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                Session["loginTime"] = DateTime.Now.ToString();// Add this line
                Session["webapp"] = "mywebapp";// Add this line
                return RedirectToLocal(returnUrl); 
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

enter image description here

Step 2. Connect your redis cache by redsmin. ( https://app.redsmin.com/)

I have register in this website for checking value.

Step 3. Run project.

enter image description here

Step 4. Check key and value in redsmin.

enter image description here

Upvotes: 1

Related Questions