简体   繁体   English

使用Razor在Asp.net MVC中显示重新登录(即注销后再次登录)上的最后一个cookie值

[英]Showing last cookie value on Relogin (i.e. Again login after logout) in Asp.net MVC with Razor

I am working on Asp.net MVC with Razor view engine with C#. 我正在使用带有C#的Razor视图引擎来开发Asp.net MVC。 I have a login form and on login I create a Cookie for userid, and on logout I remove this cookie evrything works fine yet. 我有一个登录表单,登录后为用户ID创建一个Cookie,并在注销时删除了该cookie evrything效果还不错。 but the problem starts when I Login again it still shows the value of Previous Login's userid. 但是问题是在我再次登录时开始的,它仍然显示以前登录用户名的值。

The code of creating cookie is 创建cookie的代码是

HttpCookie newCookie = new HttpCookie(cookieId, cookieValue);
newCookie.Expires = DateTime.Today.AddDays(30);
HttpContext.Current.Response.Cookies.Add(newCookie);

And to remove cookie 并删除co​​okie

if (HttpContext.Current.Request.Cookies[cookieId] != null)
{
    HttpContext.Current.Response.Cookies[cookieId].Value = null;
}

I have remove the cookie on login if there exist but still it is not working , please help me...... 我已经删除了登录时的cookie(如果存在),但仍然无法正常工作,请帮助我......

You can remove your cookie like below, 您可以按照以下方式删除Cookie,

if ( Request.Cookies["MyCookie"] != null )
{
    var c = new HttpCookie( "MyCookie" );
    c.Expires = DateTime.Now.AddDays( -1 );
    Response.Cookies.Add( c );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cookie第一次显示空值,再次到达该页面后,我可以看到cookie值。 Asp.net c# - cookie is showing null value for first time and after coming again to that page, i can see cookie value. Asp.net c# 识别登录Cookie-Asp.Net MVC - Identify Login Cookie - Asp.Net MVC MVC 4 ASP.NET中的订单列表,即控制器接收2个变量 - Order List in MVC 4 ASP.NET i.e. Controller receiving 2 variables 成功注册/登录后不显示注销按钮,ASP.NET Core 标识 - LOGOUT button not showing after successful REGISTER/LOGIN, ASP.NET Core Identity 使用Asp.Net 5 Identity 3.0注销后未删除Cookie - Cookie not deleted after logout with Asp.Net 5 Identity 3.0 ASP.NET MVC在登录/注销时更改默认路由 - ASP.NET MVC change default route on login/logout 成功登录并重定向到另一个剃须刀视图 ASP.NET Core MVC Web 应用程序后,UserIdentity 为 null - UserIdentity is null after successful login and redirection to another razor view, ASP.NET Core MVC web application 用户登录后,Asp.net登录状态未显示注销 - Asp.net Login status not saying logout after user logs in FormAuthentication .ASPXAUTH cookie在ASP.NET MVC中显示片刻后显示为空 - FormAuthentication .ASPXAUTH cookie showing null after few moments in ASP.NET MVC 登录 Asp.net 核心 mvc 后如何重定向到上次访问的页面 - How to Redirect to Last Visited page After Login in Asp.net core mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM