Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ASP.Net MVC HttpContext.User.Identity正在丢失_Android_Asp.net Mvc_Authentication_Google Chrome_Forms Authentication - Fatal编程技术网

Android ASP.Net MVC HttpContext.User.Identity正在丢失

Android ASP.Net MVC HttpContext.User.Identity正在丢失,android,asp.net-mvc,authentication,google-chrome,forms-authentication,Android,Asp.net Mvc,Authentication,Google Chrome,Forms Authentication,我有一个非常奇怪的场景,我被困在里面。我有一个ASP.Net MVC 4应用程序,我正在验证一个用户并创建一个authCookie,将其添加到响应的Cookie中,然后将其重定向到目标页面: if (ModelState.IsValid) { var userAuthenticated = UserInfo.AuthenticateUser(model.UserName, model.Password);

我有一个非常奇怪的场景,我被困在里面。我有一个ASP.Net MVC 4应用程序,我正在验证一个用户并创建一个authCookie,将其添加到响应的Cookie中,然后将其重定向到目标页面:

            if (ModelState.IsValid)
            {
                var userAuthenticated = UserInfo.AuthenticateUser(model.UserName, model.Password);

                if (userAuthenticated)
                {
                    var userInfo = UserInfo.FindByUserName(model.UserName);

                    //SERIALIZE AUTHENTICATED USER
                    var serializer = new JavaScriptSerializer();
                    var serializedUser = serializer.Serialize(userInfo);
                    var ticket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, serializedUser);
                    var hash = FormsAuthentication.Encrypt(ticket);
                    var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash) {Expires = ticket.Expiration};

                    Response.Cookies.Add(authCookie);

                    if (Url.IsLocalUrl(model.ReturnUrl) && model.ReturnUrl.Length > 1 && model.ReturnUrl.StartsWith("/") && !model.ReturnUrl.StartsWith("//") && !model.ReturnUrl.StartsWith("/\\"))
                    {
                        return Redirect(model.ReturnUrl);
                    }

                    var url = Url.Action("Index", "Course");
                    return Redirect(url);
                }

                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
这在所有浏览器中都可以正常工作。我可以登录并访问我的应用程序中的安全页面

我的客户正在请求此应用程序的android版本。所以,我想知道如何将这个应用程序转换成APK文件。我的第一次尝试是创建一个简单的index.html页面,其中包含一个针对应用程序的iframe。这在Firefox和IE9中运行良好。但是,当通过Chrome访问包含指向应用程序的iframe的index.html页面时,我通过上面的登录代码,用户被重定向到安全控制器,但安全控制器有一个自定义属性,以确保用户经过身份验证:

public class RequiresAuthenticationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated) return;
            if (filterContext.HttpContext.Request.Url == null) return;

            var returnUrl = filterContext.HttpContext.Request.Url.AbsolutePath;

            if (!filterContext.HttpContext.Request.Browser.IsMobileDevice)
            {
                filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + string.Format("?ReturnUrl={0}", returnUrl), true);
            }
            else
            {
                filterContext.HttpContext.Response.Redirect("/Home/Home", true);
            }           
        }
    }
我的应用在以下方面失败:filterContext.HttpContext.User.Identity.IsAuthenticated。IsAuthenticated始终为false,即使用户已在上面的代码中进行了身份验证

请记住,只有在通过Chrome中的iframe访问应用程序时才会发生这种情况。如果我直接访问应用程序而不是通过iframe,那么一切都正常

有什么想法吗

更新:

我的控制器扩展了SecureController。在SecureController的构造函数中,我有反序列化用户的代码:

public SecureController()
        {
            var context = new HttpContextWrapper(System.Web.HttpContext.Current);

            if (context.Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                var serializer = new JavaScriptSerializer();
                var cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName].Value;
                var ticket = FormsAuthentication.Decrypt(cookie);
                CurrentUser = serializer.Deserialize<UserInfo>(ticket.UserData);
            }
            else
            {
                CurrentUser = new UserInfo();
            }


            //if ajax request and session has expired, then force re-login
            if (context.Request.IsAjaxRequest() && context.Request.IsAuthenticated == false)
            {
                context.Response.Clear();
                context.Response.StatusCode = 401;
                context.Response.Flush();
            }
        }

首先,您应该从AuthorizeAttribute派生,而不是ActionFilterAttribute。授权属性在更高级别的管道中调用方法之前执行,而ActionFilters则在更深层次执行,其他属性可以在您的方法之前执行

其次,您没有显示用于解密票据并设置IPrincipal和IIdentity的代码。因为这就是问题所在,奇怪的是你没有把它包括进去