Swe*_*ell 5 c# asp.net-mvc iidentity
我已经开发了一个简单的IIdentity和IPrincipal我的MVC项目,我想覆盖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; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义校长:
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
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义控制器,我成功更新了User属性以返回我的自定义主体类型:
public abstract class BaseController : Controller
{
protected new virtual MyPrincipal User
{
get { return HttpContext == null ? null : HttpContext.User as MyPrincipal; }
}
}
Run Code Online (Sandbox Code Playgroud)
如何以同样的方式User.Identity返回我的自定义身份类型?
IPrincipal您可以在您的类中显式实现MyPrincipal,并添加您自己的Identitytype 属性MyIdentity。
public class MyPrincipal : IPrincipal
{
public MyPrincipal(MyIdentity identity)
{
Identity = identity;
}
public MyIdentity Identity {get; private set; }
IIdentity IPrincipal.Identity { get { return this.Identity; } }
public bool IsInRole(string role)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5537 次 |
| 最近记录: |