在Web.Config中设置ServiceStack Cookie域会导致会话ID在每个请求上更改

vm0*_*112 2 cookies session-cookies servicestack

根据ServiceStack - 域和子域的身份验证,我在httpCookies部分设置了cookie域,它实际上有效 - 它可以正确设置cookie的域.

但我注意到,一旦我将这一行添加到配置中,就会在每个请求上生成一个新的会话ID,无论用户是否已经过身份验证.

我的代码很简单,很简单.

我的应用主机代码:

public override void Configure(Funq.Container container)
{
        Plugins.Add(new AuthFeature(() => new CustomUserSession(),
            new IAuthProvider[] {        
                    new CustomCredentialsProvider(), 
                }));

        container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("10.211.55.2:6379"));
        container.Register<ICacheClient>(c => c.Resolve<IRedisClientsManager>().GetCacheClient());

        var userRep = new InMemoryAuthRepository();
        container.Register<IUserAuthRepository>(userRep);
}
Run Code Online (Sandbox Code Playgroud)

我的自定义凭据提供者

public class CustomCredentialsProvider : CredentialsAuthProvider
{
    public override bool TryAuthenticate(ServiceStack.IServiceBase authService, string userName, string password)
    {
        return true;
    }

    public override void OnAuthenticated(ServiceStack.IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        session.UserAuthName = "test user";
        session.UserName = "test user";

        authService.SaveSession(session);
    } 
}
Run Code Online (Sandbox Code Playgroud)

我的默认页面:

public partial class _Default : WebForms.App_Start.PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //get session information in every way possible
            var session = this.UserSession;

            var sessionKey = SessionFeature.GetSessionKey();

            var session2 = SessionFeature.GetOrCreateSession<CustomUserSession>(this.Cache);

            var session3 = this.Cache.Get<CustomUserSession>(sessionKey);        

        }
Run Code Online (Sandbox Code Playgroud)

将发布上面的sessionKey,并在每个请求(预期)上保持不变.添加:

<system.web>
  <httpCookies domain="localhost"/>
Run Code Online (Sandbox Code Playgroud)

导致sessionKey在每个请求中都不同; 几乎像饼干即将到期.将域名更改为"test.com"或".test.com"似乎没有什么区别.我将127.0.0.1 localhost和127.0.0.1 test.com添加到我的Windows主机文件中以获得良好的衡量标准.

同样,即使在进行身份验证之后,注释掉httpCookies行和会话ID也保持不变.添加上述行会导致会话在每个请求上发生更改,无论是否进行身份验证.

正在设置Cookie,并在每个请求中使用新的会话ID进行设置.

我可能做错了什么,我会在哪里覆盖这种行为?任何帮助表示赞赏.

Sco*_*ott 5

这看起来不像ServiceStack问题,而是浏览器不接受返回的cookie的问题,因为您的测试域是无效的选择.

localhost 不是有效的cookie域:

您不应该将域设置为localhost,因为这不是有效的cookie域.要使cookie域被视为有效,它必须包含.在其中.因此,在使用localhost时,域值必须是null.

IP地址不是有效的cookie域:

您不能将IP地址用作cookie域.因此127.0.0.1浏览器可以认为设置无效.

主机文件:

你说你设置test.com指向127.0.0.1你的主机文件.鉴于您的ServiceStack服务正在侦听环回地址,那么该域test.com应该适用于该服务,但您必须在配置中反映出来,即

<system.web>
    <httpCookies domain="test.com"/>
Run Code Online (Sandbox Code Playgroud)

您也可以直接在AppHost Configure方法中配置它:

public override void Configure(Container container)
{
    Config = new HostConfig {
        RestrictAllCookiesToDomain = "test.com",
    };
}
Run Code Online (Sandbox Code Playgroud)

如果ServiceStack正确设置了您所说的cookie,则问题必须是浏览器不接受cookie.您可以确认这是检查浏览器随请求发送的cookie.如果您没有看到在下次请求时发回的cookie,则浏览器不接受它们.你必须确保你是http://test.com来自localhost或不是来自或127.0.0.1.