使用Web API进行表单身份验证将在5分钟后过期

时间:2015-03-18 09:24:42

标签: session asp.net-web-api2 session-management

我一直在创建一个Web项目,我使用Cookie来验证用户是否进行身份验证。

一切正常,但当我在共享主机上托管时,会话在5分钟不活动后过期。我已将配置中的超时设置为60分钟。

<authentication mode="Forms">
            <forms name=".auth" protection="All" timeout="60"/>
        </authentication>

登录时,我创建了一个名为login的API,我正在设置身份验证。

FormsAuthentication.SetAuthCookie(".auth", false);

我检查用户是否在Web API中进行了身份验证,我的API看起来像。

    [Authorize]
    [RoutePrefix("api/value")]
    public class ValueController : ApiController
    {
        // GET api/value
        [Route("get")]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

    }

我知道这是通过cookie验证的一种非常古老/糟糕的方法,但这是我的遗留应用程序,我无法更改身份验证结构并使用Owin。

有人可以建议为什么它会在共享主机上闲置5分钟后到期?它在我的本地IIS上运行正常,并且到60分钟后才会到期。

帮助是适当的。

1 个答案:

答案 0 :(得分:0)

The reason is the hosting provider clears the session and the solution is to add the machineKey in web.config.

<system.web>
    <machineKey decryption="AES" decryptionKey="ddE3434J4K3J3KLNDFPSODIFSFLKJW34L3OIUF" validation="HMACSHA256" validationKey="07dfdDUIEJSLDJFKLSJFEIOUR3989F8SDF90DF9D0F9DF9SD0F90SDF09SD0F9DF" />
</system.web>

(Please note this is a sample key. You have to generate a key and add to your config)