[英]User.IsInRole error
我在Site.Master.cs
的Page_Load
中有這個代碼。
if(User.IsInRole("Read"))
{
NavigationMenu.Visible = false;
}
我收到此錯誤:
非靜態字段,方法或屬性'Microsoft.VisualBasic.ApplicationServices.User.IsInRole(string)需要對象引用。
有線索嗎?
您可以使用IsInRole方法獲取當前的HttpContext 用戶並驗證給定角色,如下所示。
HttpContext.Current.User.IsInRole("Read")
將您的方法更改為
if(HttpContext.User.IsInRole("Read"))
{
NavigationMenu.Visible = false;
}
看起來你正在使用該類而不是該類的實例嘗試:
User user = new User();
user.IsInRole("Read");
我不完全同意答案。 您正在從母版頁的Page
屬性中獲取User
實例,因此您應該使用:
var user = Page.User;
user.IsInRole("your role");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.