【问题标题】:SetAuthCookie does not set cookie on our test serverSetAuthCookie 没有在我们的测试服务器上设置 cookie
【发布时间】:2014-11-04 00:48:54
【问题描述】:

我正在尝试在新环境中设置我的网站,但我遇到了会员提供商的问题。

我可以调用Membership.ValidateUser,它应该返回真假。这是完美的。

但是,在我的新环境中,从未设置过 cookie。我可以在 localhost 和我们的生产服务器上看到它设置了一个名为 CommunityServer 的 cookie,但在我们的新环境中却没有。

Web.config 代码

<authentication mode="Forms">
      <!-- development -->
      <forms name=".CommunityServer" protection="All" timeout="60000" loginUrl="~/user/login" slidingExpiration="true"/>
      <!-- deployment -->
      <!--<forms name=".CommunityServer" domain="domain.com" protection="All" timeout="60000" loginUrl="~/user1.aspx" slidingExpiration="true" />-->
    </authentication>
    <authorization>
      <allow users="?"/>
    </authorization>

登录代码

if (String.IsNullOrEmpty(UsernameLogin)) {
                ModelState.AddModelError("UsernameLogin", Strings.Error_NoLoginUsernameEntered);
            }
            if (String.IsNullOrEmpty(PasswordLogin)) {
                ModelState.AddModelError("PasswordLogin", Strings.Error_NoLoginPasswordEntered);
            }
            if (!Membership.ValidateUser(UsernameLogin, PasswordLogin)) {
                ModelState.AddModelError("UsernameLogin", Strings.Error_LoginFailed);
            }


            if (!ModelState.IsValid) {
                return View(new UserLoginModel() { Title = String.Format(Strings.Site_Title, Strings.UserLogin_Title) });
            }

            FormsAuthentication.SetAuthCookie(UsernameLogin, true);

            // we know this code is run and I am being redirected to the return url
            if (!String.IsNullOrEmpty(ReturnUrl)) {
                return Redirect(ReturnUrl);
            }

关于为什么我们的 cookie 从未设置过的任何提示?它是一个 IIS 8 服务器。

【问题讨论】:

  • 尝试设置正确的domain 参数,就像您在 cmets 上设置的那样。

标签: c# asp.net cookies asp.net-membership iis-8


【解决方案1】:

在认证参数上加上domain="domain.com",表示cookie对全域有效,对正确的域有效,否则有可能无法设置。

<authentication mode="Forms">
      <!-- development -->
      <forms name=".CommunityServer" domain="domain.com" protection="All" timeout="60000" loginUrl="~/user/login" slidingExpiration="true"/>

【讨论】:

    猜你喜欢
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多