MVC中的条件ActionResult [英] Conditional ActionResult in MVC

查看:100
本文介绍了MVC中的条件ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个登录,从哪里检查用户名和密码是否正确?如果正确重定向或留在登录页面。我写下以下代码。



Hi,
I have a login from where check userid and password correct or not ?if correct redirect or stay in login page .for that I write bellow code .

public class LoginController : Controller
    {
       public ActionResult Index()
        {
            return View();
        }

        public ActionResult CheckUser(string UserName,string Password)
        {
           
             if ("user authenticate" == true)
            {
                return RedirectToAction("Index", "home");
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
        }
}





因此,如果用户未进行身份验证,则还会发生重定向。

是否有可能重定向只有一次。如果用户进行身份验证,则不然。



So if a user not authenticate then also a redirection occurred.
Is it possible the redirection is for one time only.if user authenticate,other wise not.

推荐答案

使用此代码进行身份验证:

use this code for Authentication:
 con.Open();
                cmd = new SqlCommand("Select * from Registration  where Email='"+Email+"' And Password='"+Pass+"'"), con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
Session["UserId"] = dt.Rows[0]["UserId"].ToString();
                

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

                }
                else
                {
                 
                  

                    return RedirectToAction("Index", "Login");
                }
                con.Close();


这篇关于MVC中的条件ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆