ASP.net MVC 获取从 url 检索到的特定 ID 的用户角色
ASP.net MVC get users Roles for specific id retrieved from the url
当用户登录时我写了这段代码:
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
}
当你使用 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
当用户登录时我写了这段代码:
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
}
当你使用 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