我正在制作一个新的(空模板(ASP.NET MVC 5应用程序,无法注销此应用程序。我的注销操作:
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)
{
}
}
连接字符串:
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=.;Database=DataTest;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
LogOff((操作执行起来没有问题,并将我重定向到"登录"操作,但我仍然登录。它怎么了?
试试这个:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
//AuthenticationManager.SignOut();
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);
Session.Abandon();
return RedirectToAction("Login", "Account");
}
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
LogoutPath = new PathString("/Account/SignOut"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
^^将Startup.Auth.cs中的">LogoutPath"设置为您想要的
我觉得你的大部分代码都很好。我猜你的操作方法出了问题。通常情况下,这里唯一要做的就是
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Login", "Account");
}
我不知道if块是否对您的注销过程至关重要,但这两行代码是您唯一需要做的事情。如果它至关重要,您应该通过调试器检查SignOut方法是否命中。
这对我很有效:在RouteConfig.cs中创建一个路由,如
routes.MapRoute(
"userlogout",
"Account/Logout",
new { controller = "Account", action = "LogOff" }
);
您可以在中维护默认注销代码AccountController.cs或添加其他人建议的添加项(如session.abandon();
等(但正如下面应该工作
[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");
}
在这种情况下,您还可以执行以下操作:从您的LogOff操作中删除[HttpPost],并放入[HttpGet]。你只需要通过AntiForgeryToken。但问题是,这是否是一种非常安全的方式。此处提供更多信息:使用MVC3';s HTTP GET中的AntiForgeryToken以避免Javascript CSRF漏洞
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Login", "Account");
}
关于ASP.Net MVC注销不工作:-
我遇到了一个问题,在生产模式下,IIS上托管的应用程序无法正确使用chrome
尽管它在所有浏览器中都使用Visual Studio Dev托管,但在IE 的生产模式下运行良好
我在Startup.Auth.CS中遇到了问题。请确保以下内容没有重复的配置
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.UseCookieAuthentication((new CookieAuthenticationOptions(.....))