Thursday, 12 December 2013

mvc page security

 i have two pages from login to details check using dictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcUpdate.EF;

namespace MvcUpdate.Controllers
{
    public class UserProfileController : Controller
    {
        //
        // GET: /UserProfile/
        test_dbEntities db = new test_dbEntities();

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Getlogin(emp obj)
        {
            string name = obj.name;
            string age = obj.age.ToString();
            if (obj.name != null)
            {

                webSession.checkLogin(name, age.ToString());
                if (webSession.checkLogin(name, age.ToString()) == null)
                {
                    return View("index");

                }
                else
                {
                    webSession.startSession(name, age);
                    TempData["key"] = name;

                    if (1 > 0)
                    {

                        return View("../Emp/index", TempData);
                    }
                    else
                    {
                        return View();
                    }

                }
            }
            else
            {
                return View("index");
            }
        }

    }
}
------------------------------------------------------------------------------------------------------------
@model MvcUpdate.EF.emp

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
@using (Html.BeginForm("Getlogin", "UserProfile", FormMethod.Post))
{

    <div>
    <table>
    <tr>
    <td>@Html.Label("User Name")</td>
    <td>@Html.TextBoxFor(m => m.name)</td>
        </tr>
       
    <tr>
    <td>@Html.Label("Password")</td>
    <td>@Html.PasswordFor(m => m.age)</td>
        </tr>
       
        <tr>
        <td>
        <input id="submit1" type="submit" />
        </td>
        </tr>
        </table>
    </div>
}
</body>
</html>
----------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcUpdate.EF;

namespace MvcUpdate.Controllers
{
    public class EmpController : Controller
    {
        //
        // GET: /Emp/
        test_dbEntities db = new test_dbEntities();
      
        [OutputCache(Duration = 0)]
        public JsonResult ShowEmployee()
        {
            if (webSession.validateSession(TempData["key"].ToString()) == null)
            {

               // return View("../UserProfile/index", TempData);
                return Json(db.emps, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var t = TempData["key"];
                return Json(db.emps, JsonRequestBehavior.AllowGet);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult UpdateEmployee(int empId, string empName, int age)
        {
            emp Emp = db.emps.Single(em => em.id == empId);
            Emp.name = empName;
            Emp.age = age;
            db.SaveChanges();
            return Json("Record has Updated", JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            int pageSize = 5;
            int pageNo = 1;
            int totalPage = db.emps.Count() / pageSize;
            ViewData["tp"] = totalPage;

            int skp = (pageNo - 1) * pageSize;

            List<emp> Emp = db.emps.OrderBy(em => em.id)
                .Skip(skp).Take(pageSize).ToList();
            return View(Emp);
        }   

      
    }
}
--------------------------------------------
------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcUpdate.EF;

namespace MvcUpdate
{
    public class WebAuthentication
    {

        string _UserName, _Role;

        public string Role
        {
            get { return _Role; }

        }

        public string UserName
        {
            get { return _UserName; }

        }
        public WebAuthentication(string userName, string role)
        {
            _UserName = userName;
            _Role = role;
        }
    }
    public class webSession
    {
        static Dictionary<string, string> _Users = new Dictionary<string, string>();
        //public static void startSession(string sessionId, WebAuthentication user)
        //{
        //    if (!_Users.ContainsKey(sessionId))
        //    {
        //        _Users.Add(sessionId, user);
        //    }
        //}
         public static WebAuthentication checkLogin(string name,string age)
        {
            test_dbEntities db = new test_dbEntities();
            int kh =Convert.ToInt32(age);
            if (age != null)
            { var p = (from k in db.emps
                       where k.name == name && k.age == kh
                     select k).ToList();
            return new WebAuthentication(name, age.ToString());

            }
            else
            {
                return null;

            }
        }
    
        public static string validateSession(string sessionId)
        {
            if (_Users.ContainsKey(sessionId))
            {
                return _Users[sessionId];
            }
            else
            {
                return null;
            }
        }
        public static void endSession(string sessionId)
        {
            if (_Users.ContainsKey(sessionId))
            {
                _Users.Remove(sessionId);
            }
        }

        internal static void startSession(string p,string age)
        {
            if (!_Users.ContainsKey(p))
            {
                _Users.Add(p,age);
            }
        }
    }
}
--------------------------------------------------------------------------------------------------------------------
add class

No comments:

Post a Comment