c# - SetAuthCookie 未在我们的测试服务器上设置 cookie

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

我正在尝试在新环境中设置我的网站,但成员(member)资格提供商存在问题。

我可以调用Membership.ValidateUser,它会返回 true 和 false,正如它应该的那样。那就完美了。

但是,在我的新环境中,cookie 从未设置。我可以在本地主机和我们的生产服务器上看到,它设置了一个名为 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="domain.com",表示cookie对整个域有效,并且对正确的域有效,否则有可能无法设置。

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

关于c# - SetAuthCookie 未在我们的测试服务器上设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25764260/

相关文章:

php - 设置 cookie 和重定向而不会出现 header 错误的语义

c# - 如何在 Xamarin Forms 中使用 pdf.js 查看 PDF

asp.net - Http 处理程序在 iis express 中工作,而在 iis 服务器中不工作

jquery - CORS、Ajax 和 CSRF

asp.net - 如何将 html 按钮设置为 ASP.Net 表单的默认按钮?

asp.net - 我可以在 ViewState 中存储 xmlDocument 对象吗?

cookies - 如何在 ColdFusion 10 中覆盖 CFID/CFTOKEN?

c# - 加入一个线程无一异常(exception)地停止执行

c# - 运行时找不到方法

c# - 使用缓冲区从折线获取多边形