【问题标题】:FormsAuthentication - handling a change of usernameFormsAuthentication - 处理用户名的更改
【发布时间】:2025-12-26 18:40:15
【问题描述】:

我的 ASP.NET MVC Web 应用程序允许管理员更改自己或其他用户的用户名。

用户通过调用FormsAuthentication.SetAuthCookie(userName [string], createPersistentCookie [bool]) 登录。他们通过调用FormsAuthentication.SignOut() 注销。我了解在更新用户名后,我需要将其注销并重新登录。但是如何检索createPersistentCookie现有 值?例如重新登录时如何保留他们原来的“记住我”设置?

【问题讨论】:

    标签: asp.net asp.net-mvc forms-authentication


    【解决方案1】:
    var cookieName = FormsAuthentication.FormsCookieName;
    var request = HttpContext.Current.Request;
    var cookie = request.Cookies.Get(cookieName);
    if (cookie == null)
        return;
    
    try
    {
        var ticket = FormsAuthentication.Decrypt(cookie.Value);
    
        //This should give you what you want...
        bool isPersistent = ticket.IsPersistent;
    }
    catch (Exception ex)
    {
        //Logging
    }
    

    【讨论】: