MVC プロジェクト用に単純なIIdentity
andを開発しました。これをオーバーライドして、適切な型の値を返しIPrincipal
たいと思います。User
User.Identity
これが私のカスタムアイデンティティです:
public class MyIdentity : IIdentity
{
public MyIdentity(string name, string authenticationType, bool isAuthenticated, Guid userId)
{
Name = name;
AuthenticationType = authenticationType;
IsAuthenticated = isAuthenticated;
UserId = userId;
}
#region IIdentity
public string Name { get; private set; }
public string AuthenticationType { get; private set; }
public bool IsAuthenticated { get; private set; }
#endregion
public Guid UserId { get; private set; }
}
これが私のカスタムプリンシパルです:
public class MyPrincipal : IPrincipal
{
public MyPrincipal(IIdentity identity)
{
Identity = identity;
}
#region IPrincipal
public bool IsInRole(string role)
{
throw new NotImplementedException();
}
public IIdentity Identity { get; private set; }
#endregion
}
これが私のカスタム コントローラーです。User
カスタム プリンシパル タイプを返すようにプロパティを正常に更新しました。
public abstract class BaseController : Controller
{
protected new virtual MyPrincipal User
{
get { return HttpContext == null ? null : HttpContext.User as MyPrincipal; }
}
}
User.Identity
カスタム ID タイプを返すのと同じようにするにはどうすればよいですか?