【问题标题】:why dont when add member to IPrincipal为什么不向 IPrincipal 添加成员
【发布时间】:2015-04-23 10:53:40
【问题描述】:

我正在向IPrincipal 添加一个成员。我用@User.LastName

我收到一个错误:

CS1061:“System.Security.Principal.IPrincipal”不包含“LastName”的定义,并且找不到接受“System.Security.Principal.IPrincipal”类型的第一个参数的扩展方法“LastName”(是您缺少 using 指令或程序集引用?)

代码:

public class LabPrincipal : IPrincipal
{
    private readonly LabIdentity _identity;

    public LabPrincipal(LabIdentity identity)
    {
        _identity = identity;
    }

    public bool IsInRole(string role)
    {
        return
            _identity.Roles.Any(
                current => string.Compare(current, role,      StringComparison.InvariantCultureIgnoreCase) == 0);
    }

    public IIdentity Identity
    {
        get { return _identity; }
    }

    public LabIdentity Information
    {
        get { return _identity; }
    }

    public bool IsUser
    {
        get { return !IsGuest; }
    }

    public bool IsGuest
    {
        get { return IsInRole("guest"); }
    }

    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

【问题讨论】:

  • 这可能是因为您有一个不知道 LastNameIPrincipal - 您可能需要将其转换为 LabPrincipal 才能访问这些附加属性

标签: asp.net-mvc


【解决方案1】:

IPrincipal 是一个没有属性“LastName”的接口。只有你的 LabPrincipal 类可以(我们可以看到)。

您必须在视图模型中将 User 转换为 LabPrincipal 才能像这样使用它。

[代码隐藏]

yourViewModel.User = LabRepository.GetLabPrincipal(<user id>);

[剃刀]

@Model.User.LastName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多