asp.net - FormsAuthenticationTicket 不存储用户数据

标签 asp.net forms-authentication

我对 .NET 比较陌生,正在尝试弄清楚 FormsAuthentication。我已经完成了一些不同的教程,每次我都会遇到同样的问题。由于某种原因,用户数据没有存储在票证中。当我在 Global.aspx.cs 中的票证上设置中断时,UserName 在那里,但 UserData 是一个空字符串,当版本指定为 1 时,版本设置为 2。另一个奇怪的事情是允许每个经过身份验证的用户当 web.config 指定只有管理员可以访问我的 Admin_Content 文件夹中的页面时。

登录.aspx

UserFull user = ManageUsers.login(loginTemplate.UserName, loginTemplate.Password);
if (user != null)
{
    string[] roles = { user.role };

    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
        1,
        userName,
        DateTime.Now,
        DateTime.Now.AddDays(30),
        true,
        roles[0],
        FormsAuthentication.FormsCookiePath
    );

    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
    FormsAuthentication.SetAuthCookie(encryptedTicket, true);

    Response.Redirect("Admin_Content/Admin.aspx");
 }

Global.aspx.cs Application_AuthenticateRequest

if (HttpContext.Current.User != null)
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        if (HttpContext.Current.User.Identity is FormsIdentity)
        {
            FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
            FormsAuthenticationTicket ticket = (id.Ticket);
            if (!string.IsNullOrEmpty(ticket.UserData))
            {
                string userData = ticket.UserData;
                string[] roles = userData.Split(',');
                HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
            }
        }
    }
}

主 web.config

<configuration>
<system.web>
  <compilation debug="true" targetFramework="4.0" />
  <roleManager enabled="true"></roleManager>
  <authentication mode="Forms">
    <forms name="AOTMP_Demo" loginUrl="Login.aspx"
    protection="All" path="/" cookieless="UseCookies"/>
  </authentication>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>
<configuration>

Admin_Content 文件夹的 web.config

<configuration>  
  <system.web>
    <authorization>
      <allow roles="Administrator"/>
      <deny users="?"/>
    </authorization>
  </system.web>
</configuration>

最佳答案

您不应该这样做...因为它会创建一个新的授权票证。

FormsAuthentication.SetAuthCookie();

而是显式设置 cookie

var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
cookie.Expires = authTicket.Expiration;
Request.Cookies.Add(cookie);

然后它将在global.asax中可用

var userData = ((FormsIdentity)HttpContext.Current.User.Identity).Ticket.UserData;

关于asp.net - FormsAuthenticationTicket 不存储用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12642516/

相关文章:

MySQL数据库与ASP.NET全局连接

c# - asp.net c# 应用程序中浏览器之间的文件扩展名丢失

css - 样式表未加载

c# - 在 asp 按钮单击事件中访问 Html TextBox 控件?

asp.net - 加密和解密 FormsAuthenticationTicket 以验证用户身份

ASP.NET MVC FormsAuthentication Cookie超时不能增加

c# - ASP.NET 2.0 站点中的 ASP.NET 4.0 应用程序能否使用相同的表单例份验证 cookie?

asp.net - T-SQL 中的 HTML 编码?

java - 基于表单的身份验证 - 身份验证成功后重定向到错误页面 (Tomcat 7.0.4)

wcf - 对 WCF 服务进行身份验证