C# 无法注销标识MVC 5应用程序
我正在制作一个新的(空模板)ASP.NET MVC 5应用程序,无法注销此应用程序。 我的注销操作:C# 无法注销标识MVC 5应用程序,c#,asp.net-mvc,asp.net-mvc-5,asp.net-identity,identity,C#,Asp.net Mvc,Asp.net Mvc 5,Asp.net Identity,Identity,我正在制作一个新的(空模板)ASP.NET MVC 5应用程序,无法注销此应用程序。 我的注销操作: public ActionResult LogOff() { if (User.Identity.IsAuthenticated) { //break here } try { AuthenticationManager.SignOut(); if (User.Identity.IsAuthenticated
public ActionResult LogOff()
{
if (User.Identity.IsAuthenticated)
{
//break here
}
try
{
AuthenticationManager.SignOut();
if (User.Identity.IsAuthenticated || Request.IsAuthenticated)
{
//break here;
}
}
return RedirectToAction("Login", "Account");
}
启动类:
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
}
应用程序上下文:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", false)
{
}
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,false)
{
}
}
连接字符串:
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=.;Database=DataTest;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
操作LogOff()执行时没有问题,并将我重定向到“登录”操作,但我仍然登录。
有什么问题吗?我觉得你的大部分代码都不错。我猜你的行动方法有问题。通常这里唯一要做的就是
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Login", "Account");
}
我不知道if块是否对您的注销过程至关重要,但这两行程序是您唯一需要做的事情。如果这很重要,您应该通过调试器检查是否命中了签出方法。尝试以下操作:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
//AuthenticationManager.SignOut();
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
Session.Abandon();
return RedirectToAction("Login", "Account");
}
这对我很有用:
在RouteConfig.cs中创建一条路线,如
routes.MapRoute(
"userlogout",
"Account/Logout",
new { controller = "Account", action = "LogOff" }
);
您可以在中维护默认的注销代码
AccountController.cs或添加其他人建议的添加内容(如session.discard();
等)
但正如下面所说,这应该是可行的
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Login", "Account");
}
这似乎对我很有效
public ActionResult Logoff()
{
HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Response.Cache.SetNoStore();
Session.Clear();
Session.Abandon();
Session.RemoveAll();
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
LogoutPath=新路径字符串(“/Account/SignOut”),
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});
^^将Startup.Auth.cs中的“LogoutPath”设置为所需的任何路径。在这种情况下,您还可以执行以下操作: 从注销操作中删除[HttpPost],并改为放置[HttpGet]。 您只需通过AntiForgeryToken即可。但问题是,这是否是一种非常安全的方式。更多信息请参见:
关于ASP.Net MVC注销不起作用:- 我遇到了一个问题,即IIS上的应用程序在生产模式下无法与chrome正常工作 尽管它在所有浏览器中使用VisualStudio Dev托管时在IE的生产模式下工作正常 我在Startup.Auth.CS中遇到问题。确保以下内容没有重复配置
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.UseCookieAuthentication((new CookieAuthenticationOptions(.....))
app.CreatePerOwinContext(ApplicationUserManager.Create);
app.UseCookieAuthentication((新的CookieAuthenticationOptions(…))
是的,我知道,我只是用另一个代码检查用户是否仍然通过身份验证。问题是,应用程序似乎从cookie或其他东西获取数据,然后我在执行此操作后仍然登录。此外,请记住,如果您使用的是Chrome,它可能不会将您注销:在执行此方法后应删除cookie(使用启动类中选择的设置)。请使用浏览器的开发工具检查cookie是否仍然存在。关于Chrome,我经常使用它进行开发,在使用Identity时还没有遇到问题。@George我也遇到过同样的问题,当我使用IE进行测试时,问题消失了。Chrome没有注销我。为什么这是一篇帖子?对不起,为什么使用[HttpPost]
。应该更详细specific@Halter-改变状态的操作不应为Get Actions。这使得某人作为恶作剧注销非常容易,因为某人添加了一个指向/Logoff的
标记。
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Login", "Account");
}
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.UseCookieAuthentication((new CookieAuthenticationOptions(.....))