title: ASP.NET MVC 5 IdentityUsers has Post. How can I assign posts to user?
body: <p>I want to create something like a blog application. User signs in and can add a post. This post is assigned to him (there is a relation one to many). The post can have comments. I am using scaffold to create an PostsController. It's partially working. It is creating a new post, but it's not assigning it to the signed in user. My problem is I cannot assign this post to the user.
  When in previous appication I was creating two models, I could make such a relation. Is it different using IdentityModel? Is there any other way to reference the current logged in User?</p>
  
  <p>I am using following Post model:</p>
  
  <p><strong>Conference/Conference/Models/Post.cs</strong></p>
  
  <pre><code>namespace Conference.Models
  {
      public class Post
      {
          public int Id { get; set; }
  
          [Required]
          [StringLength(255)]
          public string Title { get; set; }
  
          public string Body { get; set; }
  
          public int ApplicationUserId { get; set; }
  
          public ApplicationUser ApplicationUser { get; set; }
  
          public IEnumerable&lt;Post&gt; Posts { get; set; }
  
  
      }
  }
  </code></pre>
  
  <p>Here is my <strong>IdentityModels.cs</strong></p>
  
  <pre><code>namespace Conference.Models
  {
      // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
      public class ApplicationUser : IdentityUser
      {
          [Required]
          [StringLength(100)]
          public string FirstName { get; set; }
  
          [Required]
          [StringLength(100)]
          public string LastName { get; set; }
  
          [Required]
          public bool IsReviewer { get; set; }
  
          [Required]
          public bool IsModerator { get; set; }
  
          public IEnumerable&lt;Post&gt; Posts { get; set; }
  
  
           public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
          {
              // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
              var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
              // Add custom user claims here
              return userIdentity;
          }
      }
  
      public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public DbSet&lt;Post&gt; Posts { get; set; }
          public DbSet&lt;Comment&gt; Comments { get; set; }
  
          public ApplicationDbContext()
              : base("DefaultConnection", throwIfV1Schema: false)
          {
          }
  
          public static ApplicationDbContext Create()
          {
              return new ApplicationDbContext();
          }
      }
  }
  </code></pre>
  
  <p><strong>Views/Posts/Create.cshtml</strong></p>
  
  <pre><code>using Conference.Models
  @using Microsoft.AspNet.Identity
  @using Microsoft.AspNet.Identity.EntityFramework
  @model Conference.Models.Post
  
  @{
      ViewBag.Title = "Create";
      Layout = "~/Views/Shared/_Layout.cshtml";
  
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  
  @using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Post&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Title, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Title, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Body, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Body, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Body, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
          @Html.HiddenFor(m=&gt; m.ApplicationUserId)
  
  
  
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>
  
  <p><strong>PostsController</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Net;
  using System.Web;
  using System.Web.Mvc;
  using Conference.Models;
  
  namespace Conference.Controllers
  {
      public class PostsController : Controller
      {
          private ApplicationDbContext db = new ApplicationDbContext();
  
          // GET: Posts
          public ActionResult Index()
          {
              return View(db.Posts.ToList());
          }
  
          // GET: Posts/Details/5
          public ActionResult Details(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              Post post = db.Posts.Find(id);
              if (post == null)
              {
                  return HttpNotFound();
              }
              return View(post);
          }
  
          // GET: Posts/Create
          public ActionResult Create()
          {
              return View();
          }
  
          // POST: Posts/Create
          // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
          // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create([Bind(Include = "Id,Title,Body,ApplicationUserId")] Post post)
          {
              if (ModelState.IsValid)
              {
                  db.Posts.Add(post);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              return View(post);
          }
  
          // GET: Posts/Edit/5
          public ActionResult Edit(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              Post post = db.Posts.Find(id);
              if (post == null)
              {
                  return HttpNotFound();
              }
              return View(post);
          }
  
          // POST: Posts/Edit/5
          // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
          // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Edit([Bind(Include = "Id,Title,Body,ApplicationUserId")] Post post)
          {
              if (ModelState.IsValid)
              {
                  db.Entry(post).State = EntityState.Modified;
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
              return View(post);
          }
  
          // GET: Posts/Delete/5
          public ActionResult Delete(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              Post post = db.Posts.Find(id);
              if (post == null)
              {
                  return HttpNotFound();
              }
              return View(post);
          }
  
          // POST: Posts/Delete/5
          [HttpPost, ActionName("Delete")]
          [ValidateAntiForgeryToken]
          public ActionResult DeleteConfirmed(int id)
          {
              Post post = db.Posts.Find(id);
              db.Posts.Remove(post);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          protected override void Dispose(bool disposing)
          {
              if (disposing)
              {
                  db.Dispose();
              }
              base.Dispose(disposing);
          }
      }
  }
  </code></pre>