如何在OWINASP.NETMVC5中注销用户我有一个用于ASP.NETMVC5项目的标准AccountController类。当我尝试注销用户时,我遇到了错误,因为HttpContext为空。(我的意思是HttpContext.GetOwinContext()。authenticationisnull)所以我不明白我们如何在会话结束时注销用户...在global.asax中我得到了这个protectedvoidSession_Start(objectsender,EventArgse){Session.Timeout=3;}protectedvoidSession_End(objectsender,EventArgse){try{varaccountController=newAccountController();accountController.SignOut();}catch(Exception){}}AccountControllerpublicvoidSignOut(){//即使我这样做也无济于事,因为HttpContext为NULL_authnManager=HttpContext.GetOwinContext().Authentication;AuthenticationManager.SignOut();私人IAuthenticationManager_authnManager;//添加此私有变量publicIAuthenticationManagerAuthenticationManager//将其从私有修改为公共并添加Manager_ag{agnull)_authnManager=HttpContext.GetOwinContext().Authentication;返回_authnManager;}设置{_authnManager=值;}}Startup.Auth.cs有publicvoidConfigureAuth(IAppBuilderapp){//启用应用程序使用cookiee为登录用户存储信息为此,您需要定义一个ActionFilter属性,并且需要将用户重定向到相应的控制器操作,您需要在其中检查会话值,如果它为空,则需要重定向用户。这是代码(您也可以访问我的博客了解详细信息):字符串actionName=filterContext.ActionDescriptor.ActionName.ToLower().Trim();if(!actionName.StartsWith("login")&&!actionName.StartsWith("sessionlogoff")){varsession=HttpContext.Current.Session["SelectedSiteName"];HttpContextctx=HttpContext.Current;//如果会话超时,将用户重定向到登录屏幕if(session==null){base.OnActionExecuting(filterContext);filterContext.Result=newRedirectToRouteResult(newRouteValueDictionary(new{controller="Account",action="SessionLogOff"}));}}}}}假设您使用ApplicationCookie来存储登录信息。AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);调用Session_End()会导致异常。这完全是预料之中的,因为您不能简单地创建新的AccountController()、调用accountController.SignOut()并期望它起作用。这个新控制器没有连接到MVC管道——它没有HttpContext和所有其他能够工作的要求。您应该注销用户以响应他们提出的请求。使用个人帐户身份验证创建新的MVC项目。打开AccountController并查看LogOff()方法:[HttpPost][ValidateAntiForgeryToken]publicActionResultLogOff(){AuthenticationManager.SignOut();返回RedirectToAction("索引","主页");}AuthenticationManager.SignOut()将在此处执行以响应/Account/LogOff的POST请求。每当这样的请求到达时,ASP.NET/MVC将创建一个AccountController实例并正确初始化它。之后,调用LogOff方法,您可以在其中实际执行AuthenticationManager.SignOut();。还有,带有Identity的ASP.NET/MVC应用程序默认在代码的Helpers区域声明AuthenticationManager是这样的:privateIAuthenticationManagerAuthenticationManager{希望这对您有所帮助。我尝试了这一切:System.Web.HttpContext.Current.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);FormsAuthentication.SignOut();AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);Request.GetOwinContext().Authentication.SignOut();Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);但最后解决了我的问题:HttpContext.User=newGenericPrincipal(newGenericIdentity(string.Empty),null);校试Session.Abandon();varowinContext=System.Web.HttpContext.Current.Request.GetOwinContext();varauthenticationTypes=owinContext.Authentication.GetAuthenticationTypes();owinContext.Authentication.SignOut(authenticationTypes.Select(o=>o.AuthenticationType).ToArray());“`这对我有用`publicvoidSignOut(){IOwinContextcontext=_context.Request.GetOwinContext();IAuthenticationManagerauthenticationManager=上下文。身份验证;authenticationManager.SignOut(AuthenticationType);}`我唯一的问题是没有重定向到登录,所以我得到一个未找到视图的错误,因为我注销的视图是在[Authorization]属性下我认为当用户没有被这个代码块授权时,内置自动重定向...以上是C#学习教程:如何在OWINASP.NETMVC5中注销用户分享的所有内容。如果对大家有用,需要了解更多C#学习教程,希望大家多多关注——`app.UseCookieAuthentication(newCookieAuthenticationOptions{AuthenticationType="ApplicationCookie",LoginPath=newPathString("/Account/Login"),ExpireTimeSpan=TimeSpan.FromHours(1),});`本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
