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



我想创建一个类似博客应用程序的东西。用户登录并可以添加帖子。这个职位分配给他(有一对多的关系)。帖子可以有评论。我正在使用脚手架来创建PostsController。它部分工作。它正在创建新帖子,但不会将其分配给登录用户。我的问题是我无法将此帖子分配给用户。 在以前的应用程序中,当我创建两个模型时,我可以建立这样的关系。使用身份模型有什么不同吗?有没有其他方法可以引用当前登录的用户?

我正在使用以下帖子模型:

会议/会议/模型/帖子.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; }

}
}

这是我的身份模型.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();
}
}
}

Views/Posts/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")
}

帖子控制器

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

通过在Microsoft.AspNet.Identity中引用;您可以使用

string CurrentUser = User.Identity.Name 

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

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

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

public string ApplicationUserId { get; set; }

现在,在您看来,您正在添加@Html.HiddenFor(m=> m.ApplicationUserId)这是一个隐藏字段,但如果用户修改 HTML,他们仍然可以实际修改其值,因此最好避免这种情况。您应该做的是删除它并删除Create操作结果中ApplicationUserId的绑定,并在提交表单后使用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);
}

最新更新