如何传递价值?

时间:2014-02-14 11:46:07

标签: asp.net-mvc-4 asp.net-routing

请帮帮我..这是我的登录代码,我将在会话中存储用户名和角色ID的值,这只返回bool值,如何传递角色会话的值ID ? { public ActionResult LogIn(FSLIndiaLatest.Models.Tbl_Users用户)

         {
                //int userid = user.UserID;
                //var sessionid=Session["userid"];
                Session["RoleId"] = user.RoleId;
                Session["Username"] = user.UserName;
                var sessionval = Session["Username"].ToString();
                if (ModelState.IsValid)
                {
                    if (Isvalid(user.UserName, user.UserPassword, user.RoleId))
                    {

                        Session["RoleId"] = user.RoleId;
                        int sessionid = Convert.ToInt32(Session["RoleId"]);

                        FormsAuthentication.SetAuthCookie(user.UserName, false);

                        string status = Session["Username"].ToString();
                        if (status == "admin")
                        {
                            //  return RedirectToAction("adminpage", "UserLogIn");
                            return RedirectToAction("adminpage", "LogIn");
                        }
                        else
                        {
                            return RedirectToAction("userpage", "LogIn");
                        }

                    }
                    return View(sessionval);
                }
                return View();
            }
    }



{
private bool Isvalid(string username, string password, int RoleId)
        {
            bool Isvalid = false;
            using(var db = new FSLIndiaLatest.Models.FSLWebEntitiesContext())
            {
                var user = db.Tbl_Users.FirstOrDefault(u => u.UserName == username);
                //var user = db.Tbl_Users.FirstOrDefault(u => u.UserName == username);
                var pass = db.Tbl_Users.FirstOrDefault(u => u.UserPassword == password);
                if (username != null)
                {
                    try
                    {
                        if (user.UserName == username)
                        {
                            if (pass.UserPassword == password)
                            {
                                Isvalid = true;
                                Session["RoleId"] = user.RoleId;
                                int sessionid = Convert.ToInt32(Session["RoleId"]);
                            }
                        }
                    }
                    catch
                    {
                        //Response.Write("Login Failed For The User");
                        Isvalid = false;
                    }

                }

            }

            return Isvalid;
        }
}

{

此代码正常运行,我已尝试过会话。

{

    [HttpGet]
            [AllowAnonymous]
            public ActionResult LogIn()
            {
                return View();
            }

            [HttpPost]
            [AllowAnonymous]
            public ActionResult LogIn(FSLIndiaLatest.Models.Tbl_Users user)
            {
                //int userid = user.UserID;
                //var sessionid=Session["userid"];
                Session["RoleId"] = user.RoleId;
                Session["Username"] = user.UserName;
                var sessionval = Session["Username"].ToString();
                if (ModelState.IsValid)
                {
                    if (Isvalid(user.UserName, user.UserPassword))
                    {
                        var db = new FSLIndiaLatest.Models.FSLWebEntitiesContext();
                        var userroleid = db.Tbl_Users.FirstOrDefault(u => u.UserName == user.UserName);


                        Session["RoleId"] = userroleid.RoleId;
                        int sessionroleid = Convert.ToInt32(Session["RoleId"]);

                        FormsAuthentication.SetAuthCookie(user.UserName, false);

                        string sessionusername = Session["Username"].ToString();
                        if (sessionroleid == 1)
                        {
                            //  return RedirectToAction("adminpage", "UserLogIn");
                            return RedirectToAction("adminpage", "LogIn");
                        }
                        else
                            if(sessionroleid==2)
                            {
                                return RedirectToAction("teammanager", "LogIn");
                            }
                            else
                        {
                            return RedirectToAction("userpage", "LogIn");
                        }

                    }
                    return View(sessionval);
                }
                return View();
            }


    }
}

1 个答案:

答案 0 :(得分:0)

使用HttpCookie保存用户登录数据。在基础控制器中,在cookie类型中定义HttpCookie字段。然后使用它。设定值:

 cookie = this.Request.Cookies.Get("cookie_Name");

 if (cookie != null)
 {
    //UserName
    cookie.Values.Set("Username", user.UserName);
    //RoleId
    cookie.Values.Set("RoleId", user.RoleId);
 }
 else
 {
    cookie = new HttpCookie("cookie_Name");
 }

 this.Response.Cookies.Add(cookie);

然后得到如下值:

 cookie = this.Request.Cookies.Get("cookie_Name");

 if (cookie != null)
 {
    //UserName
    var username = Convert.ToString(cookie.Values.Get("Username"));
    //RoleId
    var roleId = Convert.ToInt32(cookie.Values.Get("RoleId"));
 }