简体   繁体   English

如何在视图中访问HttpContext.Current.User自定义属性

[英]How to access HttpContext.Current.User custom attributes in the view

I want to access some custom attributes in my view from a User object. 我想从User对象访问视图中的一些自定义属性。 I am implementing custom attributes for authentication and I am changing the HttpContext.Current.User inside of my global.asax. 我正在实现用于身份验证的自定义属性,并且正在我的global.asax中更改HttpContext.Current.User

This is the User class 这是User

public class User : IPrincipal
{
    ...
    public bool IsAdministrator => IsInRole(RolesConstants.GlobalAdministrator);
    ...
}

Here is where I am setting it in my Global.asax 这是我在Global.asax设置的地方

    protected void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
    {
        ...

        var winUser = new User
        {
            EMail = user.Person.Email,
            FirstName = user.Person.FirstName,
            LastName = user.Person.LastName,
            Identity = wi,
            NetworkAccountName = user.UserName,
            UserId = user.UserName,
            Roles = userRoles,
        };

        HttpContext.Current.User = winUser;
    }

As an example how can I do something like this? 例如,我该怎么做?

<button type="button" visible="@User.IsAdministrator" id="btn"></button>

Since the User object is already accessible I don't want to pass in a Model or use a string in the view such as @User.IsInRole("Admin") 由于User对象已经可以访问,因此我不想传递Model或在视图中使用字符串,例如@User.IsInRole("Admin")

Edit: should I be making a custom type derive from IPrinciple and explose the types like this? 编辑:我应该使自定义类型派生自IPrinciple并展开像这样的类型吗?

...
        IIdentity Identity { get; }
        bool IsInRole(string role);
        bool IsAdministrator;
...

You need to cast the User property to your custom class: 您需要将User属性强制转换为自定义类:

@{
    var user = User as MyNamespace.User; // MyNamespace is the namespace of your User class
}

<button type="button" visible="@user.IsAdministrator" id="btn"></button>

[Edit] [编辑]

Another quick-and-dirty solution using an extension method: 另一个使用扩展方法的快捷解决方案:

public static class ViewUserExtensions {

    public static User ToCustom(this IPrincipal principal)
    {
        return principal as User;
    }
}

<button type="button" visible="@User.ToCustom().IsAdministrator" id="btn"></button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在mvc c中将HttpContext.Current.User作为自定义主体# - How to get HttpContext.Current.User as custom principal in mvc c# HttpContext.Current.User对于无状态会话是否安全? - Is HttpContext.Current.User Safe for Stateless Session? HttpContext.Current.User和HttpContext.User有什么区别 - What is the difference between HttpContext.Current.User and HttpContext.User ASP.Net MVC 5-在控制器外部访问HttpContext.Current.User的正确方法? - ASP.Net MVC 5 - Correct way to access HttpContext.Current.User outside of controller? HttpContext.Current.User 和 this.User.Identity 的区别 - Difference between HttpContext.Current.User and this.User.Identity HttpContext.Current.User未填充启用Windows身份验证 - HttpContext.Current.User not populated with Windows Authentication enabled 如何在 MVC 中的 Application_PostAuthenticateRequest 之后引用 HttpContext.Current.User? - How do I reference the HttpContext.Current.User after the Application_PostAuthenticateRequest in MVC? 角色添加到 HttpContext.current.user 但 isInRole(rolename) 不起作用 - Role added to HttpContext.current.user but isInRole(rolename) is not working 在异步等待中使用 HttpContext.Current.User 的正确方法 - Correct way to use HttpContext.Current.User with async await 从Thread.CurrentPrincipal设置HttpContext.Current.User - Set HttpContext.Current.User from Thread.CurrentPrincipal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM