ASP.net MVC 获取从 URL 检索的特定 ID 的用户角色
本文关键字:用户 角色 ID 检索 MVC net 获取 URL ASP | 更新日期: 2023-09-27 18:37:08
当用户登录时,我编写了以下代码:
FormsAuthentication.SetAuthCookie(form.username, true);
return RedirectToAction("Index", "Users",new { id = myUser.userid });
现在在用户控制器中,我有此操作来检索 url 中特定 id 的所有数据
[Authorize(Roles = "user")]
public ActionResult Index(int id)
{
return View(x.psostTBLs.Where(n => n.post_userid == id).ToList());
}
现在,每个登录用户都可以访问所有其他用户的页面
示例:如果我以用户 100 身份登录,我可以访问网站中的所有其他页面并编辑其数据
本地主机:3343/用户/100
本地主机:3343/用户/200
本地主机:3343/用户/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
}
当你使用Identity时,你可以在你的视图中像这样做:
@if(User.IsInRole("canEdit"))
{
edit
delete
}
else
{
only i can read the content
}
"可以编辑"这是角色的名称。
请阅读这里:http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1