【问题标题】:ASP.net MVC get users Roles for specific id retrieved from the urlASP.net MVC 获取从 url 检索到的特定 id 的用户角色
【发布时间】:2016-06-15 19:02:51
【问题描述】:

当用户登录时我写了这段代码:

FormsAuthentication.SetAuthCookie(form.username, true);
return RedirectToAction("Index", "Users",new { id = myUser.userid });

现在在 UserController 中,我有这个操作可以检索 url 中特定 id 的所有数据

    [Authorize(Roles = "user")]
    public ActionResult Index(int id)
    {
       return View(x.psostTBLs.Where(n => n.post_userid == id).ToList());
    }

现在每个登录用户都可以访问所有其他用户页面

示例:如果我以用户 100 身份登录,我可以访问网站中的所有其他页面并编辑其数据

localhost:3343/user/100

localhost:3343/user/200

localhost:3343/user/300

在视图中我需要这样的东西:

if(i'm logged in as user 100 and i'm in page localhost:3343/user/100)
{
edit
delete
.... etc
}
else if(i'm logged in as user 100 and i'm in page localhost:3343/user/200 or any other page)
{
only I can read the content
}

【问题讨论】:

    标签: c# asp.net-mvc model-view-controller roles


    【解决方案1】:

    当你使用 Identity 时,你可以在你的视图中这样做:

     @if(User.IsInRole("canEdit"))
        {
    edit
    delete
        }
        else
        {
    only i can read the content
        }
    

    “canEdit”是角色的名称。

    请阅读这里:http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 2015-07-17
      • 2021-12-27
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多