Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在mvc中获取HttpContext.Current.User作为自定义主体#_C#_.net_Model View Controller - Fatal编程技术网

C# 如何在mvc中获取HttpContext.Current.User作为自定义主体#

C# 如何在mvc中获取HttpContext.Current.User作为自定义主体#,c#,.net,model-view-controller,C#,.net,Model View Controller,我正在尝试将HttpContext.Current.User与我自己的类一起使用,该类继承自IPrincipal。我不太清楚原因 var lIdentity = new UserIdentity(lFormsTicket); var lRoles = lAuthUser.Roles.Select(x => x.Role.ToString()).ToArray(); var lPrincipal = new GenericPrincipal(lIdentity, lRoles); Syst

我正在尝试将
HttpContext.Current.User
与我自己的类一起使用,该类继承自
IPrincipal
。我不太清楚原因

var lIdentity = new UserIdentity(lFormsTicket);
var lRoles = lAuthUser.Roles.Select(x => x.Role.ToString()).ToArray();
var lPrincipal = new GenericPrincipal(lIdentity, lRoles);

System.Web.HttpContext.Current.User = lIdentity;

在我设置好之后,它就开始工作了。但是如果我在网站上再做一个请求,它会说它不能投。我在这里遗漏了什么吗?

您正在将
User
设置为
IIdentity
,而此时它应该是
IPrincipal

var lIdentity = new UserIdentity(lFormsTicket);
var lRoles = lAuthUser.Roles.Select(x => x.Role.ToString()).ToArray();
var lPrincipal = new GenericPrincipal(lIdentity, lRoles);

System.Web.HttpContext.Current.User = lPrincipal;
public class UserPrincipal : IPrincipal {
    string[] roles;

    public UserPrincipal (IIdentity identity, param string[] roles) {
        this.Identity = identity;
        this.roles = roles ?? new string[]{ };
    }

    public IIdentity Identity { get; private set; }

    public bool IsInRole(string role) {
        return roles.Any(s => s == role);
    }
}
您可能需要使用自己的自定义
i原则

var lIdentity = new UserIdentity(lFormsTicket);
var lRoles = lAuthUser.Roles.Select(x => x.Role.ToString()).ToArray();
var lPrincipal = new GenericPrincipal(lIdentity, lRoles);

System.Web.HttpContext.Current.User = lPrincipal;
public class UserPrincipal : IPrincipal {
    string[] roles;

    public UserPrincipal (IIdentity identity, param string[] roles) {
        this.Identity = identity;
        this.roles = roles ?? new string[]{ };
    }

    public IIdentity Identity { get; private set; }

    public bool IsInRole(string role) {
        return roles.Any(s => s == role);
    }
}

我不确定应用程序是否会抱怨,因为它可能需要
ClaimsPrincipal
派生类型。

这也不起作用。(BO.Security.UserIdentity)User.Identity会导致相同的问题。抛出错误的位置。还显示您的
BO.Security.UserIdentity
类,因为我们需要知道它继承自什么。请注意,
GenericPrincipal.Identity
=>
获取由当前System.Security.Principal.GenericPrincipal
表示的用户的System.Security.Principal.GenericEntity。除了设置用户的方法之外,在我尝试将user.Identity获取为自定义useridentity的任何地方都会抛出错误(在登录中).UserIdentity继承自IIdentity
GenericPrincipal.Identity
返回一个
GenericEntity
,而不管您赋予它派生的
IIdentity
如何。它基本上将您赋予它的任何内容封装在一个
GenericEntity
中,这就是您尝试强制转换它失败的原因。您必须创建自己的
IPrincipal
派生类。