Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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# Asp.Net访问外部程序集中的声明标识_C#_Asp.net Web Api_Asp.net Identity_Claims Based Identity_Claims - Fatal编程技术网

C# Asp.Net访问外部程序集中的声明标识

C# Asp.Net访问外部程序集中的声明标识,c#,asp.net-web-api,asp.net-identity,claims-based-identity,claims,C#,Asp.net Web Api,Asp.net Identity,Claims Based Identity,Claims,我正在实现一个Asp.NETWebAPI应用程序,我在声明中存储一些特定于用户的值 我的项目由几个类库项目组成,其中包含模型和控制器 现在,我尝试访问主项目(在类库中)控制器之外的声明 //calling this from a class library var identity = Thread.CurrentPrincipal.Identity; //No user information such as Id available var claims = ClaimsPrincipal.

我正在实现一个Asp.NETWebAPI应用程序,我在声明中存储一些特定于用户的值

我的项目由几个类库项目组成,其中包含模型和控制器

现在,我尝试访问主项目(在类库中)控制器之外的声明

//calling this from a class library
var identity = Thread.CurrentPrincipal.Identity; //No user information such as Id available
var claims = ClaimsPrincipal.Current.Claims; //Contains zero elements
这里的声明包含零元素。但如果我把上面的代码放在主项目的控制器中,一切都会正常工作


如何从外部程序集访问声明?

经过一些调查后,我删除了
SuppressDefaultHostAuthentication()来自启动配置类。

现在
Thread.CurrentPrincipal.Identity
可以按预期工作。

最好只传递标识,而不是依赖于无法控制的静态成员。使您的库也可测试。@我可以使用'Thread.CurrentPrincipal.identity;'访问identity对象但我看不到它的用户信息。你能用简单的例子来回答吗?在你的web API中,只需像这样调用你的库:
myCustomLibrary.DoSomething(currentPrincipal,otherArgument)
–也就是说,将主体传递给你的方法。我只需阅读类库控制器方法中的声明。所以我需要访问
CurrentPrincipal
对象。所以我想我不能采纳你的建议。如果调用
mainsassembly.GetCurrentPrincipal()
,可以吗?这是一种好的做法吗?还是其他解决方案?