FormsAuthentication - 处理用户名的变化 [英] FormsAuthentication - handling a change of username

查看:117
本文介绍了FormsAuthentication - 处理用户名的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET MVC Web应用程序,管理员可以改变自己,或者其他用户的用户名。

用户可以通过调用记录在 FormsAuthentication.SetAuthCookie(用户名[字符串] createPersistentCookie [布尔])。他们是通过调用注销 FormsAuthentication.SignOut()。据我所知,在更新后的用户名我需要签署出来,并再次回。但我怎么检索的现有 createPersistentCookie 的价值?例如我该怎么办保留其原有的记住我设置登录他们回来的时候?


解决方案

  VAR cookieName = FormsAuthentication.FormsCookieName;
VAR请求= HttpContext.Current.Request;
VAR饼干= request.Cookies.Get(cookieName);
如果(饼干== NULL)
    返回;尝试
{
    VAR票= FormsAuthentication.Decrypt(cookie.Value);    //这应该给你你想要的...
    布尔isPersistent = ticket.IsPersistent;
}
赶上(异常前)
{
    //记录
}

My ASP.NET MVC web application allows administrators to change their own, or other users' usernames.

Users are logged in by calling FormsAuthentication.SetAuthCookie(userName [string], createPersistentCookie [bool]). They are logged out by calling FormsAuthentication.SignOut(). I understand that after updating the username I'd need to sign them out and back in again. But how do I retrieve the existing value of createPersistentCookie? e.g. how do I retain their original 'remember me' setting when signing them back in?

解决方案

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
}

这篇关于FormsAuthentication - 处理用户名的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆