【发布时间】:2019-11-29 20:09:06
【问题描述】:
我已经在 .net mvc 中做过很多次了,所以这应该很容易。
我需要做的就是引用我创建的“Helper”文件夹中的一个方法来清理我的代码。
我在文件夹Helpers中有这个方法。
using Microsoft.AspNetCore.Mvc;
using mocHub2.Models;
using mocHub2.Models.Enterprise;
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Threading.Tasks;
namespace mocHub2.Helpers
{
public class UserIdentity
{
private static EnterpriseContext enterpriseDB = new EnterpriseContext();
private SLGContext db = new SLGContext();
public List<int> IdentitySearch()
{
object newObject = new object();
bool StoreManagerAccess = false;
List<int> StoreIdList = new List<int>();
UserPrincipal user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain, System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName), IdentityType.SamAccountName, System.Security.Principal.WindowsIdentity.GetCurrent().Name);
// Check to see if the user is in the Permissions table
if (db.Permissions.Any(x => x.Email == user.EmailAddress))
{
StoreManagerAccess = true;
// If exists add StoreId to List<int> StoreIdList
db.Permissions.Where(x => x.Email == user.EmailAddress).ToList().ForEach(x => { StoreIdList.Add(x.StoreId); });
}
var newlist = new HashSet<decimal>();
var ListFromPermissionsCheck = db.Permissions.Where(x => x.Email == user.EmailAddress).ToList();
return StoreIdList;
}
}
}
我在另一个文件 EnterpriseController.cs 中,我正在尝试引用此方法。
List<int> storeIdList = UserIdentity.IdentitySearch();
UserIdentity.IdentitySearch() 下有一条红线表示
object reference required for non static field
我可以将我的方法设为静态,但它会给方法中的 db 部分带来相同的错误。
我需要做什么才能引用该方法?
【问题讨论】:
-
是的。谢谢尤金