Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Asp.net mvc 4 如何纠正成功后用户被重定向到登录,而不是用户页面的问题?_Asp.net Mvc 4_C# 4.0_Custom Membershipprovider - Fatal编程技术网

Asp.net mvc 4 如何纠正成功后用户被重定向到登录,而不是用户页面的问题?

Asp.net mvc 4 如何纠正成功后用户被重定向到登录,而不是用户页面的问题?,asp.net-mvc-4,c#-4.0,custom-membershipprovider,Asp.net Mvc 4,C# 4.0,Custom Membershipprovider,在MVC4中,我创建了一个自定义成员资格提供程序,如果用户身份验证通过,它将返回true。这里没什么大不了的-这部分的工作方式应该是: public override bool ValidateUser(string username, string password) { var crypto = new SimpleCrypto.PBKDF2(); // type of encryption // TODO: using (var unitOf

在MVC4中,我创建了一个自定义成员资格提供程序,如果用户身份验证通过,它将返回true。这里没什么大不了的-这部分的工作方式应该是:

    public override bool ValidateUser(string username, string password)
    {
        var crypto = new SimpleCrypto.PBKDF2(); // type of encryption
        // TODO: using (var unitOfWork = new Website.Repository.UnitOfWork(_dbContext))
        //var unitOfWork1 = new Website.Repository.UnitOfWork(_dbContext);

        using (var db = new Website.DAL.WebsiteDbContext())
        {
            var user = db.Users
                .Include("MembershipType")
                .FirstOrDefault(u => u.UserName == username);
            if (user != null && user.Password == crypto.Compute(password, user.PasswordSalt))
            {
                FormsAuthentication.SetAuthCookie(username, true);
                return true;
            }
        }
        return false;
    }
在我的登录操作中:

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Login(Models.UserModel user)
    {
        if (ModelState.IsValid)
        {
            // custom membership provider
            if (Membership.ValidateUser(user.UserName, user.Password))
            {
                // Cannot use this block as user needs to login twice
                //if (User.IsInRole("WaitConfirmation"))  // checks the custom role provider and caches based on web.config settings
                //{
                //    //TempData["EmailAddress"] = thisUser.Email;

                //    // email address has not yet been confirmed
                //    return RedirectToAction("WaitConfirmation");
                //    //return View("Account", thisUser)
                //}
                //else
                //{
                //    // get custom identity - user properties
                //    string userName = UserContext.Identity.Name;
                //    //CustomIdentity identity = (CustomIdentity)User.Identity;
                //    var identity = UserContext.Identity;
                //    int userId = identity.UserId;

                //    return RedirectToAction("Index", "Dashboard");
                //}

                if (User.Identity.IsAuthenticated && User.IsInRole("WaitConfirmation"))  // checks the custom role provider and caches based on web.config settings
                {
                    return RedirectToAction("WaitConfirmation");
                }
                else if (User.Identity.IsAuthenticated)
                {
                    // get custom identity - user properties
                    string userName = UserContext.Identity.Name;

                    return RedirectToAction("Index", "Dashboard");
                }
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect.");
            }
        }

        return View(user);
    }
在单步执行代码时,当用户首次登录时,
user.Identity.IsAuthenticated
为false,页面将重定向回登录页面。此时,如果我:

  • 手动导航到用户页面(仪表板),用户信息可用
  • 再次登录,这是有效的
我相信答案就在于为什么
User.Identity.IsAuthenticated
不是立即
true
的,但是第一次就无法确定这是假的

注释掉的第一块代码失败,
无法将类型为“System.Security.Principal.GenericEntity”的对象强制转换为类型为“Website.AdminWebsite.Infrastructure.CustomIdentity”
,因为没有对
进行身份验证的检查


建议?

这篇文章描述了一个类似症状的问题


请仔细阅读并确保事件的顺序(即,Authenticate,LoggedIn)

在阅读了@mcsilvio建议的文章后,我添加了一个
重定向操作()
,如下所示,以启动新的页面生命周期:

    public ActionResult Login(Models.UserModel user)
    {
        if (ModelState.IsValid)
        {
            // custom membership provider
            if (Membership.ValidateUser(user.UserName, user.Password))
            {
                return RedirectToAction("VerifyIdentity", user);
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect.");
            }
        }

        return View(user);
    }

    public ActionResult VerifyIdentity(Models.UserModel user)
    {
        if (User.Identity.IsAuthenticated && User.IsInRole("WaitConfirmation"))  // checks the custom role provider and caches based on web.config settings
        {
            return RedirectToAction("WaitConfirmation");
        }
        else if (User.Identity.IsAuthenticated)
        {
            // get custom identity - user properties
            string userName = UserContext.Identity.Name;

            return RedirectToAction("Index", "Dashboard");
        }

        return View(User);
    }

这确实起到了作用,但我想知道是否有更好的方法,或者总是这样做?

这是解决方案之一,但为什么需要额外的页面访问。应该有办法。所以他们说基本上不需要检查User.Identity.IsAuthenticated,因为很明显,如果Membership.ValidateUser是真的,那么他们就被验证了?我明天得试试这个。