1 Replies - 5948 Views - Last Post: 30 October 2013 - 03:57 PM

#1 Chario0z   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 12

Simple login system - Printing user information

Posted 30 October 2013 - 09:48 AM

I'm working on a simple, custom login system where users can make an account and log in. In this system I'm using Sessions to make sure if user is logged in or not. I'm using MVC4 as well with Entity Framework.

Right now, I want to make a function where users can change their data (name, adress, phone, password). Also, when you log in, you will see your name, last login time and a link to an edit page where you're able to edit your data.

The problem is that I'm not sure how I can retrieve the information/data about the user after they log in. Let me show you some code and explain underway.

This is my UserModel. Since I'm using MVC, I have a controller and View-pages too.

public class User {
    public int ID {get; set;}

    [Required(ErrorMessage = "Specify you email adresse")]
    public string email {get; set;} // this will be the user name

    [Required(ErrorMessage = "Specify your name")]
    public string name {get; set;}

    [Required(ErrorMessage = "Specify your adresse")]
    public string adresse {get; set;}

    [Required(ErrorMessage = "Specify your password")]
    public string password {get; set;}
}

public class dbUser {
    [Key]
    public int ID {get; set;}
    public string email {get; set;}
    public string name {get; set;}
    public string adresse {get; set;}
    public byte[] password {get; set;}
    public Datetime lastLoginTime {get; set;}

}

public class DatabaseContext : DbContext
// Having my Context class with public DbSet<dbUser> Users;



Since I'm using MVC4, I do have a Controller and View-pages. My Controller looks like this (not all of the code, but you'll get the idea):

public class UserController : Controller {

    public ActionResult LogIn() {
        if (Session["LoggedIn"] == null)
            {
                Session["LoggedIn"] = false;
                ViewBag.LoggedIn = false;
            }
            else
            {
                ViewBag.LoggedIn = (bool)Session["LoggedIn"];
            }

            return View();
    }

    public ActionResult LogIn(FormCollection input) {
        string email = input["Email"];
        string password = input["Password"]; // These are from my input fields. 

        if(userOK(email, password)) { // This method checks if the user exits. Won't write the method here. 
            Session["LoggedIn"] = true;
            ViewBag.LoggedIn = true;
            return RedirectToAction("MyPage");
        }
        else {
            Session["LoggedIn"] = false;
            ViewBag.LoggedIn = false;
            return View();
        }
    }

    public ActionResult MyPage {
        if(Session["LoggedIn"].equals(true)) {
            return View();
        }
        return RedirectToAction("LogIn");
    }
}



This is just an illustration of how my Controller looks like. When we log in, we check the session and if the user exits. If it does, it will redirect to MyPage.

And now my problem. When I get to MyPage, I want to be able to give the user a Welcome "YourName" message.
For example *Welcome Bob! Your last login time was 30.10.2013 13:37. *

The thing is that I'm not sure how I can get access to these data from my View. So I can't go to the edit details page.

My MyPage-View looks like this:

@model Project.Models.dbUser
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>My Page</title>
</head>
<body>
    <div>
        <h2>Welcome @Models.name</h2>
        <p>Your last login time was @Model.lastLoginTime </p>


        @Html.ActionLink("Edit your details", "Edit", new { id = @Model.ID }) // Want to edit my details. 

        <form action="/User/Logout">
            <input type="submit" value="Log out" />
        </form>

    </div>
</body>
</html>



The thing is that none of the @Model.XX works. I get an NullReferenceException error or something. It seems like it doesn't know what @Model.name, @Model.lastLogInTime, @Model.ID is.

Why won't it work? How can I solve this problem?

Also, what's the easiest way to edit the details? Right now my plan was to send the ID as parameter to the Edit-method in controller, find the user with the ID i sent as paramter and edit data. Easier way to do it?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple login system - Printing user information

#2 Chario0z   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 23-April 12

Re: Simple login system - Printing user information

Posted 30 October 2013 - 03:57 PM

Edit: I fixed the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1