ASP.NET MVC 5 IdentityUsers有Post。如何为用户分配帖子?

时间:2017-04-25 14:23:04

标签: c# asp.net asp.net-mvc

我想创建类似博客应用程序的东西。用户登录并可以添加帖子。这篇文章被分配给他(有一对多关系)。帖子可以有评论。我正在使用scaffold创建一个PostsController。它部分工作。它正在创建一个新帖子,但它没有将其分配给已登录的用户。我的问题是我无法将此帖子分配给用户。 在之前的应用程序中,我创建了两个模型,我可以建立这样的关系。使用IdentityModel有所不同吗?有没有其他方法可以引用当前登录的用户?

我正在使用以下Post模型:

会议/会议/型号/ Post.cs

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<Post> Posts { get; set; }


    }
}

这是我的 IdentityModels.cs

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<Post> Posts { get; set; }


         public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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<ApplicationUser>
    {
        public DbSet<Post> Posts { get; set; }
        public DbSet<Comment> Comments { get; set; }

        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

查看/帖子/ Create.cshtml

using Conference.Models
@using Microsoft.AspNet.Identity
@using Microsoft.AspNet.Identity.EntityFramework
@model Conference.Models.Post

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";

}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Post</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Body, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Body, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Body, "", new { @class = "text-danger" })
            </div>
        </div>
        @Html.HiddenFor(m=> m.ApplicationUserId)




        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

PostsController

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);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

引用 Microsoft.AspNet.Identity; 你可以用

{{1}}

查找当前登录用户的内容 然后,您可以访问在ApplicationUser中添加的所有其他属性

答案 1 :(得分:0)

我建议不要创建您在上述模型中创建的任何模型属性,并在控制器级别使用User.Identity.Name并将值(用户名/ ID)保存到所需的表中。(帖子您方案中的表格)

答案 2 :(得分:0)

ApplicationUserId中的Post属性应该是string,因为默认情况下IdApplicationUser所以你应该进行以下更改:

public string ApplicationUserId { get; set; }

现在,在您看来,您正在添加@Html.HiddenFor(m=> m.ApplicationUserId)这是一个隐藏字段,但如果用户修改HTML,用户仍然可以实际修改其值,因此最好避免这种情况。您应该删除该内容并删除ApplicationUserId操作结果中Create的绑定,并使用User.Identity.GetUserId();在提交表单后设置帖子的用户ID。要实现这一点,首先必须添加using Microsoft.AspNet.Identity;作为using指令。

您的Create方法应如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Title,Body")] Post post)
{
    if (ModelState.IsValid)
    {
        var userId = User.Identity.GetUserId();
        post.ApplicationUserId = userId;
        db.Posts.Add(post);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(post);
}