C#Asp.net标识将当前用户添加到模型
我试图在一个简单的模型上创建一个测试。我不知道如何将用户附加到正在提交的模型。我尝试了几种不同的方法,但似乎无法让模型提交C#Asp.net标识将当前用户添加到模型,c#,asp.net,asp.net-mvc,asp.net-identity,C#,Asp.net,Asp.net Mvc,Asp.net Identity,我试图在一个简单的模型上创建一个测试。我不知道如何将用户附加到正在提交的模型。我尝试了几种不同的方法,但似乎无法让模型提交 public class StockController : Controller { private ApplicationDbContext db = new ApplicationDbContext(); public StockController() { } // GET: Stock [Authorize(
public class StockController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public StockController()
{
}
// GET: Stock
[Authorize(Roles = "canEdit")]
public ActionResult Index()
{
var currentUserId = User.Identity.GetUserId();
var userStocks = db.Stocks.Where(p => p.User.Id == currentUserId);
//var test1 = userStocks.ToList();
return View(userStocks.ToList());
}
// GET: Stock/Details/5
[Authorize(Roles = "canEdit")]
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Stock stock = db.Stocks.Find(id);
if (stock == null)
{
return HttpNotFound();
}
return View(stock);
}
// GET: Stock/Create
[Authorize(Roles = "canEdit")]
public ActionResult Create()
{
return View();
}
// POST: Stock/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]
[Authorize(Roles = "canEdit")]
public ActionResult Create([Bind(Include = "StockId,Name,Ticker")] Stock stock)
{
//var currentUserId = User.Identity.GetUserId();
if (ModelState.IsValid)
{
//var user = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
// var user = db.Users.Select(p => p.UserName == User.Identity.GetUserName()).FirstOrDefault();
stock.User = user;
db.Stocks.Add(stock);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(stock);
}
以及应用程序用户模型
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Stock> Stocks { 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;
}
}
公共类应用程序用户:IdentityUser
{
公共虚拟ICollection存储{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
用户管理器使用的对象实例与控制器不同。因此EF无法正确跟踪用户对象。要解决此问题,只需如下更改Stock
类:
public class Stock
{
// other members here
public string UserID { get; set; }
public virtual ApplicationUser User { get; set; }
}
public ActionResult Create([Bind(Include = "StockId,Name,Ticker")] Stock stock)
{
if (ModelState.IsValid)
{
stock.UserID = User.Identity.GetUserId();
db.Stocks.Add(stock);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(stock);
}
现在更改回发创建操作方法,如下所示:
public class Stock
{
// other members here
public string UserID { get; set; }
public virtual ApplicationUser User { get; set; }
}
public ActionResult Create([Bind(Include = "StockId,Name,Ticker")] Stock stock)
{
if (ModelState.IsValid)
{
stock.UserID = User.Identity.GetUserId();
db.Stocks.Add(stock);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(stock);
}
用户管理器使用的对象实例与控制器不同。因此EF无法正确跟踪用户对象。要解决此问题,只需如下更改
Stock
类:
public class Stock
{
// other members here
public string UserID { get; set; }
public virtual ApplicationUser User { get; set; }
}
public ActionResult Create([Bind(Include = "StockId,Name,Ticker")] Stock stock)
{
if (ModelState.IsValid)
{
stock.UserID = User.Identity.GetUserId();
db.Stocks.Add(stock);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(stock);
}
现在更改回发创建操作方法,如下所示:
public class Stock
{
// other members here
public string UserID { get; set; }
public virtual ApplicationUser User { get; set; }
}
public ActionResult Create([Bind(Include = "StockId,Name,Ticker")] Stock stock)
{
if (ModelState.IsValid)
{
stock.UserID = User.Identity.GetUserId();
db.Stocks.Add(stock);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(stock);
}