locked
I have two type of login User and admin using mvc5 code first using entity framework RRS feed

  • Question

  • User-471420332 posted

    I have two type of login User and admin using mvc5 code first using entity framework. Now my User part login and functionally is completed working live successfully. And admin function is now i have to do, admin will see all user submitted applications and admin will approve that application.

    my question is old technology web forms, i did separate master page for user and admin and I used two login pages for user and admin in older technology

    Here how i can achieve best way, shall i have to use single login page for both user and admin by using session i have to redirect user dashboard and admin dashboard.

    Or I have create two website for user and admin, like below

    User:  www.user.com

    admin:  www.admin.user.com

    why i am telling to create two URL for user and admin because while uploading any new patches of admin files user should not effect, because every time user be online, Please suggest.

    Tuesday, August 28, 2018 1:11 PM

Answers

  • User475983607 posted

    We are stopping  whole site. not any of role. My manager given permission to stop whole site and upload patches.

    OK so the problem is solved...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2018 2:09 PM

All replies

  • User475983607 posted

    The common approach is a single login and using roles.  Try going through the Getting Started with Identity docs.

    https://www.asp.net/mvc/overview/security

    Tuesday, August 28, 2018 3:01 PM
  • User-471420332 posted

    If i will use single login base on roles then while uploading new patches of admin functionality user will get effect because they are submitting application online and again i have to stop website. Please suggest.

    And one more thing i am using custom login using code first approach.

    Here in this line i am getting role Id as below. RoleID=1 for user and RoleID=2 for admin 

    var RoleId = int.Parse(db.LoginInformations.Where(c => c.UserId == UserName).Select(c => c.RoleId).SingleOrDefault());

    Below is my login table structure with data

    Sno     UserId     Password      RoleId
    1         mazhar      hii                 1
    2         admin       admin            2

    I am not understanding how to redirect base on RoleID to different pages like if user will login means user dashboard page and if admin login admin dashboard page, both should be different pages

    Below is my full  login method code.

    public ActionResult Login([Bind(Include = "Username,Password")] LoginPageVM loginpage, FormCollection frm)
    {


    if (ModelState.IsValid)
    {

    LoginPageVM lp = new LoginPageVM();
    lp.Username = frm["Username"];
    lp.Username = frm["Password"];


    string UserName = frm["Username"];
    string Password = frm["Password"];

    var cl = db.LoginInformations.FirstOrDefault(c => c.UserId == UserName && c.Password == Password);


    var VerifyStatus = (from Register in db.Registers
    where Register.Username == UserName
    select Register.Status).SingleOrDefault();

    if (cl != null)
    {
    if (VerifyStatus == "Verified")
    {
    var RoleId = int.Parse(db.LoginInformations.Where(c => c.UserId == UserName).Select(c => c.RoleId).SingleOrDefault());

    var RoleName = (from Role in db.Roles
    where Role.RoleId == RoleId
    select Role.RoleName).SingleOrDefault();

    bool isPersistent = false;

    Session["UserName"] = UserName;

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(2880), isPersistent, RoleName, FormsAuthentication.FormsCookiePath);
    string hash = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

    if (ticket.IsPersistent)
    {
    cookie.Expires = ticket.Expiration;
    }
    Response.Cookies.Add(cookie);
    var ProgramApplyFor = (from Reg in db.Registers where Reg.Username == UserName select Reg.ProgramApplyFor).FirstOrDefault();
    var RegistrationId = (from Reg in db.Registers where Reg.Username == UserName select Reg.RegistrationId).FirstOrDefault();
    var CompanyName = (from Reg in db.Registers where Reg.Username == UserName select Reg.CompanyName).FirstOrDefault();
    Session["CompanyName"] = CompanyName;

    this.Session["ProgramApplyFor"] = ProgramApplyFor;
    EventLog EL = new EventLog();
    EL.LoginId = frm["Username"];
    EL.Event = "Login";
    EL.IP = HttpContext.Request.UserHostAddress; ;

    DateTime time = DateTime.Now;
    EL.TimeofEvent = time;
    this.db.EventLogs.Add(EL);
    this.db.SaveChanges();

     

    }

     

    Tuesday, August 28, 2018 4:32 PM
  • User1724605321 posted

    Hi mazhar khan ,

    You can redirect user to different page using :

    RedirectToAction("actionName", "controllerName");

    And since you know user is admin when roleid is 2 , you can try something like :

    if (RoleId==1)
    {
      RedirectToAction("UserDashboardActionName", "UsercontrollerName");
    }
    else
    {
      RedirectToAction("AdminDashboardActionName", "AdmincontrollerName");
    }

    I would suggest using ASP.NET Identity in MVC Application :

    https://www.asp.net/identity 

    https://code.msdn.microsoft.com/ASPNET-MVC-5-Security-And-44cbdb97 

    Best Regards,

    Nan Yu

    Wednesday, August 29, 2018 2:12 AM
  • User-471420332 posted

    If i will use single login base on roles then while uploading new patches of admin functionality user will get effect because they are submitting application online and again i have to stop website. Please suggest.

    And one more thing i am using custom login using code first approach.

    Wednesday, August 29, 2018 5:01 AM
  • User475983607 posted

    mazhar khan india

    If i will use single login base on roles then while uploading new patches of admin functionality user will get effect because they are submitting application online and again i have to stop website. Please suggest.

    Your question shows that you still do not understand the basics. As explained in your other thread, CS code is compiled to a single DLL.  To achieve this requirement, you must build two separate web applications.   

    mazhar khan india

    And one more thing i am using custom login using code first approach.

    Can you explain why this statement is relevant?  Is there something about the "Custom Login" control that affects roles?

    Wednesday, August 29, 2018 10:42 AM
  • User-471420332 posted

    What do mean by you must build two separate web applications.  your means I have to create new project for admin?

    Already i did publish procedure using below url and in local i am able to do publish successfully and i have to upload this patches into live server.

    http://www.compilemode.com/2017/09/publishing-asp-net-mvc-application.html

    Wednesday, August 29, 2018 11:24 AM
  • User475983607 posted

    What do mean by you must build two separate web applications.  your means I have to create new project for admin?

    That is correct.  This is the third time explaining the concept.  In MVC, the cs code is compiled to a single dll.  If you make any change to the code, regardless of the user's role, the entire application is affected, Basically, the app restarts.  The only way your requirement will work is if the admin application is separated from the user application.

    Wednesday, August 29, 2018 11:43 AM
  • User-471420332 posted

    As you told above I already started : The common approach is a single login and using roles in single application only because my manager is telling we will stop the application while adding new admin functionality patches, now i can not create new project for admin? please suggest

    Wednesday, August 29, 2018 1:23 PM
  • User475983607 posted

    As you told above I already started : The common approach is a single login and using roles in single application only because my manager is telling we will stop the application while adding new admin functionality patches, now i can not create new project for admin? please suggest

    In the old days of web forms, we had Web Site projects which allowed you to update a single page without affecting the others.  That not the case in MVC as the CS files are compiled into a single DLL.  There is no way to shut down an admin controller while leaving a user controller running unless there are two separate sites.  

    I recommend that you have a meeting with your manager and senior developer get assistance or get clarification. 

    Wednesday, August 29, 2018 1:44 PM
  • User-471420332 posted

    We are stopping  whole site. not any of role. My manager given permission to stop whole site and upload patches.

    Wednesday, August 29, 2018 1:52 PM
  • User475983607 posted

    We are stopping  whole site. not any of role. My manager given permission to stop whole site and upload patches.

    OK so the problem is solved...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2018 2:09 PM