mpen
mpen

Reputation: 282845

How to get user object and save it with model?

I've created my incredibly simplistic model:

public class ImageModel
{
    public int Id { get; set; }
    public string FileName { get; set; }
}

And now I want to store the logged-in user with the record. Presumably, I would do this by adding another property:

public User User { get; set; }

Which Visual Studio is telling me is telling me is in

using System.Web.Providers.Entities;

Assuming that's the right User class that corresponds with the presently authenticated user, how do I save that with my model?

My Create action looks like this:

    [HttpPost]
    public ActionResult Create(ImageModel imagemodel)
    {
        if (ModelState.IsValid)
        {
            db.ImageModels.Add(imagemodel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(imagemodel);
    }

I imagine I would want to add something like

 imagemodel.User = User;

Just above db.ImageModels.Add, but those appear to be two different types. The User object in Controller appears to be an IPrincipal which really doesn't seem to hold much information at all.

How do I get the rest of the user data for the authenticated user? What do I need to assign imagemodel.User to? Is that even the way to do it, or do I need to explicitly tell it I just want to save the User ID (I'm assuming this much it could figure out) -- and if so, how do I keep a User object on my model such that it points to the real User object (not just an ID), and how do I get the User ID for the currently logged in user?

Upvotes: 0

Views: 1867

Answers (1)

shannon
shannon

Reputation: 8774

It is dependent upon what technology you are using to manage logons or sessions.

Inside the controller method, you probably just want to set your model property to the value in 'User.Identity.Name', which is a string value.

That assumes the user is logged in and that you have forms authentication configured. You've probably previously authenticated the user, and given them a token (basically just an encrypted cookie) containing the value of '.Name', via the FormsAuthentication.SetAuthCookie method.

So, to keep things very simple, your Image model should probably just have a Username string property. If you have access to the user identity table, you might want to store a reference to the related user instead.

public class ImageModel
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public string Username { get; set; }
}

Controller...

[HttpPost]
public ActionResult Create(ImageModel imagemodel)
{
    if (ModelState.IsValid && User.Identity.IsAuthenticated)
    {
        imagemodel.Username = User.Identity.Name;
        db.ImageModels.Add(imagemodel);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

The interfaces exposed without knowing what provider you are using are very minimal.

IPrinicipal User
    bool IsInRole(string role)
    IIdentity Identity
        string AuthenticationType
        bool IsAuthenticated
        string Name

That's it.

Once you select a provider or decide to implement a custom one there's a whole range of SO articles that will fall into your lap.

You may also be better served looking for ASP.NET references than MVC references when researching this topic.

Upvotes: 2

Related Questions