ASP.Net 4.5注销/登录失败
本文关键字:登录 失败 注销 Net ASP | 更新日期: 2024-10-21 06:36:21
我正在构建一个ASP。NET 4.5应用程序(而不是MVC另一个大故事)使用。网络身份验证是最新的Visual Studio 13。昨天我做了一些软件包管理器更新,其中包括一些微软身份验证更新。有一段时间一切都很好。当天晚些时候,我突然无法注销,没有任何错误。在网上搜索解决方案,但一无所获。今天早上一切正常,下午晚些时候注销再次停止。在自动登录最后一个用户的Chrome中,注销将不起作用。在不自动登录的IE中,登录/注销(在Chrome停止工作后)工作了一段时间,但突然登录停止了。我试过关闭IE,重新启动我的电脑,但没有成功。作为一名程序员,这种工作/不工作的行为令人不安。
我的注销代码在注销页面中protected void Page_Load(对象发送方,EventArgs e){
上下文GetOwinContext()。身份验证。SignOut();一场放弃();
//Context.GetOwinContext().Authentication.SignOut();
//FormsAuthentication.SignOut();
//HttpContext.Current.GetOwinContext().Authentication.SignOut();
//Session.Abandon();
//Roles.DeleteCookie();
//FormsAuthentication.SignOut();
//Session.Clear();
//Session.RemoveAll();
//Session.Abandon();
Response.Redirect("~/Default.aspx", true);
}
我在评论中留下来展示我尝试过的所有不同的东西。当注销工作时,未注释的一个可以很好地工作,就像在IE中我可以登录时一样。
当它失败时,我会在Chrome中得到这个错误,每次发生时我都会得到4次。我怀疑这是有关联的。
System.Web.HttpException (0x80004005): Server cannot append header after HTTP headers have been sent.
在系统中。网状物HttpHeaderCollection。SetHeader(字符串名称、字符串值、布尔替换)在系统中。网状物HttpHeaderCollection。Set(字符串名称,字符串值)在微软。Owin。主办SystemWeb。CallHeaders。AspNetResponseHeaders。Set(String键,String[]值)在微软。Owin。主办SystemWeb。CallHeaders。AspNetResponseHeaders.set_Item(字符串键,String[]值)在微软。Owin。基础设施OwinHelpers。SetHeaderUnmodified(IDictionary 2 headers, String key, String[] values)
at Microsoft.Owin.Infrastructure.OwinHelpers.AppendHeaderUnmodified(IDictionary
2个标头,String键,String[]值)在微软。Owin。HeaderDictionary。AppendValues(字符串键,String[]值)在微软。Owin。基础设施ChunkingCookieManager。AppendResponseCookie(IOwinContext上下文、字符串键、字符串值、CookieOptions选项)在微软。Owin。安全饼干。Cookie身份验证处理程序.d_f.MoveNext()
在代码中的任何地方,try/catch语句中都没有重定向。我解决了这个问题。
这是我的启动,几乎只是默认的一个。
public partial class Startup {
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301883
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
//ExpireTimeSpan = new System.TimeSpan(14, 0, 0, 0),
SlidingExpiration = false,
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
OnException = context => {
Debug.Log(this, "CookieAuthenticationProvider exception: 'n" + context.Exception.ToString());
}
}
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
SlidingExpiration是True,我试着False看看它会有帮助,因为某个地方有一条评论,它没有帮助。这一切都很神秘,登录/注销失败没有例外。我想知道这可能与饼干有关。
在这一点上,我想知道我是否可以信任Owin,如果它能把这种神秘的失败从蓝色中拉出来——当它失败时,我正在处理完全无关的表格——它有效/无效。身份验证必须为我开发工作,更不用说发布产品了。任何关于替代身份验证系统的建议都是受欢迎的,我想我可以写一个更简单的系统,但我没有时间(至少我可以调试它)。
George
using Microsoft.AspNet.Identity;
protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
{
Context.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
Session.Abandon();
}
清除cookie名称.AspNet.ApplicationCookie