0
public class Auth
{
private IBaseRepository<User> users;
public Auth(IBaseRepository<User> users)
{
this.users = users;
}
private const string UserKey = "simpleBlog.Atuh.UserKey";
public static User User
{
get
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
return null;
var user = HttpContext.Current.Items[UserKey] as User;
if(user == null)
{
user = users.GetAll().FirstOrDefault(u => u.Name == HttpContext.Current.User.Identity.Name);
if (user == null)
return null;
HttpContext.Current.Items[UserKey] = user;
}
return user;
}
}
}
我在這部分有一個錯誤,但我不知道如何解決它。對象引用是必需的與構造函數的非靜態字段
user = users.GetAll().FirstOrDefault(u => u.Name == HttpContext.Current.User.Identity.Name);