2016-03-02 66 views
2

当我写了这个代码的用户的loggedIn:在UserController中ASP.net MVC获得用户的角色从网址检索特定ID

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

现在我有这样的行动,检索特定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 
} 

回答

0

当您使用身份证时,您可以这样做:

@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

+0

感谢名单及其工作 –

+0

如果你喜欢,最多投票请! :d –

相关问题