我有一个这样实现IPrincipal的自定义类:
public class UserPrincipal : IPrincipal
{
private readonly User _user;
public UserPrincipal(IIdentity identity, User user)
{
_user = user;
Identity = identity;
}
public bool IsInRole(string role)
{
return _user.Permissions.Any(x => x.Name == role);
}
public IIdentity Identity { get; }
}这是向AutoFac注册它的正确方式吗?
builder.RegisterType<IPrincipal>().As<UserPrincipal>();我收到一个错误:
类型‘'MyNamespace.Identity.UserPrincipal'.’的异常发生在Autofac.dll中,但在用户代码附加信息中没有处理:类型'System.Security.Principal.IPrincipal‘不能分配给服务Autofac.dll
发布于 2015-12-21 11:15:17
你必须改变你的注册码。注册具体类型(RegisterType<T>)并使用接口类型进行映射(As<T>)。
builder.RegisterType<UserPrincipal>().As<IPrincipal>();https://stackoverflow.com/questions/34394314
复制相似问题