简体   繁体   English

检查登录用户是否与登录用户相同

[英]Check if logged in user, is same as logged in

I'm developing a forum and trying to create a "edit page" , but I do only want to be able that the user that created the post can see the "edit control" and be the only user who can be able to edit his page. 我正在开发一个论坛并尝试创建一个“编辑页面” ,但是我只希望能够使创建该帖子的用户能够看到“编辑控件”,并且是唯一能够编辑其帖子的用户。页。 How should i do that? 我该怎么办?

This is how far i am right now: 这就是我现在所走的距离:

@if (WHAT TO TYPE HERE)
{
    @Html.ActionLink("Edit", "Edit", "Threads", new { @id = Model.Id }, null)
}

ANSWERE ADDED IN COMMENT 答复中添加了评论

You need to compare the current user to the original author of the post. 您需要将当前用户与帖子的原始作者进行比较。 I suppose you have a database? 我想您有一个数据库?

Let's say you have a model for a forum thread / post: 假设您有一个论坛主题/帖子模型:

public class ForumPost
{
    public int Id { get; set; }
    public string Author { get; set; }
    // [...] Additional fields.
}

The Author field should for example contain the username of the one that created the post. 例如,“ Author字段应包含创建帖子的用户名。 When viewing a post you should: 查看帖子时,您应该:

  1. Retrieve this from your repository 从您的存储库中检索
  2. Compare the current user HttpContext.Current.User.Identity.Name to the author of the post Model.Author 将当前用户HttpContext.Current.User.Identity.Name与发布Model.Author的作者进行Model.Author

If you want to do this in your view, you can do it like this: 如果要在自己的视图中执行此操作,则可以执行以下操作:

@if (HttpContext.Current.User.Identity.Name.Equals(Model.Author))
{
    @Html.ActionLink("Edit", "Edit", "Threads", new { @id = Model.Id }, null)
}

I don't have a compiler with me, but this code should work. 我没有随身携带的编译器,但是这段代码应该可以工作。

I would however not recommend doing this directly in the view. 但是,我不建议直接在视图中执行此操作。 You should create a view model which contains all the necessary fields to satisfy your view. 您应该创建一个视图模型,其中包含满足您的视图的所有必要字段。

If not already done somewhere else, first check whether the user is authenticated, then consider what kind of authentication your are using (you should give more details in your question). 如果尚未在其他地方完成,请首先检查用户是否已通过身份验证,然后考虑您使用的是哪种身份验证(您应该在问题中提供更多详细信息)。 For example, if you use windows authentication the property "User.Identity.Name" contains also the domain 例如,如果您使用Windows身份验证,则属性“ User.Identity.Name”还包含域

if (HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.Identity.Name.Equals(Model.Author))

Doing this check in the view is perfectly fine as far as you have in mind this has purely a UX function: the role of this switch should only be to make visible a button, you should not give any security concern to it. 在视图中进行此检查就完全可以了,只要您记住它仅具有UX功能:此开关的作用应仅是使按钮可见,而您不应对此进行任何安全性考虑。

The security of "is the user allowed to edit the post" has always to be checked backend in the edit controller action, where you'll have to do this check again. 必须始终在编辑控制器操作中检查“是否允许用户编辑帖子”的安全性,您必须在该操作中再次进行此检查。 Always check who is able to do an action at the beginning of the Get method for that action. 始终在Get方法的开头检查该人能够执行某项操作。

Allright thanks for your answeres but i found a other way, the way i did was 好的,谢谢您的回答,但是我找到了另一种方式,我的方式是

Controler: 控制器:

    public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Thread thread = db.Threads.Find(id);
            string userId = User.Identity.GetUserId();
            if (thread == null || thread.ApplicationUserId != userId)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryId = new SelectList(db.Categorys, "Id", "Title", thread.CategoryId);
            return View(thread);
        }
        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Title,Content,CategoryId")] Thread thread)
        {
            if (ModelState.IsValid)
            {
                Thread t = db.Threads.Include(m => m.ApplicationUser).FirstOrDefault(m => m.Id == thread.Id);
                t.Content = thread.Content;
                t.Title = thread.Title;
                db.Entry(t).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Post", "Threads", new { @id = thread.Id });
            }
            return View(thread);
        }

View: 视图:

@if (Model.ApplicationUserId == User.Identity.GetUserId())
        {
            @Html.ActionLink("Edit", "Edit", "Threads", new { @id = Model.Id }, null)
        }

That worked :) 那工作:)

Thanks for all your answeres 谢谢你的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM