這應該很簡單,但我完全不能在所有的Google搜索後找到它。這是我想要的。我有一個我想要授權的自定義用戶表(目前沒有角色)。爲此,我必須實現一個我已經完成的自定義成員資格提供程序。我的問題是,如何去設置一個自定義的IPrincipal到HttpContext.Current.User?應該在哪裏發生?比方說,我有以下設置:在ASP.NET MVC中使用窗體身份驗證的自定義IPrincipal
public class CustomMembershipProvider : MembershipProvider
{
private IUserRepository _repository;
public CustomMembershipProvider(IUserRepository repository)
{
_repository = repository;
}
public override bool ValidateUser(string username, string password)
{
//check user repository
}
//additional methods omitted for brevity
}
然後我有一個自定義用戶類的工具IPrincipal
和IIdentity
public class CustomUser : IPrincipal
{
public CustomUser(string name, int userId, string additionalInfo)
{
Identity = new CustomIdentity(name, userId, additionalInfo);
}
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
return true;
}
}
public class CustomIdentity : IIdentity
{
public CustomIdentity(string name, int userId, string additionalInfo)
{
Name = name;
UserId = userId;
AdditionalInfo = additionalInfo;
}
public string Name { get; private set; }
public string AuthenticationType
{
get { return "CustomAuth"; }
}
public bool IsAuthenticated
{
get { return !String.IsNullOrEmpty(Name); }
}
public int UserId { get; private set; }
public string AdditionalInfo { get; private set; }
}
所以我的問題是,這裏是設置Context.User正確的位置到這個自定義用戶的實例?我需要一個自定義的授權屬性嗎?如果是這樣,那會是什麼樣子?
最後你是如何得到這個的?我自己也在做類似的事情。 – ETFairfax
我在同一條船上,幾乎已經準備好創建一個擴展方法來完成這項工作! –