返回RedirectToAction不起作用

时间:2020-02-19 09:30:09

标签: asp.net-mvc return-type

enter image description here

我已经简单地检查了名称并通过(= true)

返回RedirectToAction(“ Index”,“ Home”);时--->不起作用

我不知道什么错!没有错误显示

请帮助。。谢谢很多。

2 个答案:

答案 0 :(得分:0)

尝试一下:

LoginController.cs


using System.Data.Odbc;
using System.Web.Configuration;

        public ActionResult LoginReadData()
        {
            string query = "Select * from login where name=@name and password=@password";

            //using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString))
                OR
            using (OdbcConnection connection = new OdbcConnection("Your Connection String")) //here you need to add connection 
            {
                OdbcCommand command = new OdbcCommand(query, connection);

                connection.Open();

                OdbcDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    string name = reader[0].ToString();
                    string password = reader[1].ToString();

                    if (name != null && password != null)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        return View("Create", "Registration");
                    }
                }
                reader.Close();
            }
            return View();
        }

答案 1 :(得分:0)

可能有效。

但是,您很可能已激活Cookie身份验证或类似身份验证,这意味着由于找不到身份验证Cookie,MVC会将用户重定向回登录页面。

返回重定向之前,您必须创建cookie。

如何执行此操作取决于您正在运行的MVC版本。

这里是MVC5: How to set a Auth cookie in asp.net 5

return RedirectToAction("Index", "Home");

之前添加以下内容
   // Which claims depends on what kind of information you want to store about the user
   var claims = new[] { new Claim(ClaimTypes.Name, name) };
    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    Context.Authentication.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

此处提供了一个ASP.NET Core MVC示例:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-3.1