簡體   English   中英

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身份登錄,則可以訪問網站上的所有其他頁面並編輯其數據

本地主機: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
}

使用身份時,可以在視圖中執行以下操作:

 @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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM