Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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# 在ApiController操作中获取当前用户,而不将userID作为参数传递_C#_Asp.net_Asp.net Web Api_Asp.net Identity - Fatal编程技术网

C# 在ApiController操作中获取当前用户,而不将userID作为参数传递

C# 在ApiController操作中获取当前用户,而不将userID作为参数传递,c#,asp.net,asp.net-web-api,asp.net-identity,C#,Asp.net,Asp.net Web Api,Asp.net Identity,如何在安全的ApiController操作中获取当前用户,而不将用户名或用户ID作为参数传递 我们假设这是可用的,因为我们在一个安全的操作中。处于安全操作中意味着用户已经通过身份验证,并且请求具有她的承载令牌。鉴于WebApi已授权用户,可能有一种内置的方式来访问用户ID,而无需将其作为操作参数传递 string userName; string userId; if (HttpContext.Current != null && HttpContext.Current.User

如何在安全的ApiController操作中获取当前用户,而不将用户名或用户ID作为参数传递

我们假设这是可用的,因为我们在一个安全的操作中。处于安全操作中意味着用户已经通过身份验证,并且请求具有她的承载令牌。鉴于WebApi已授权用户,可能有一种内置的方式来访问用户ID,而无需将其作为操作参数传递

string userName;
string userId;
if (HttpContext.Current != null && HttpContext.Current.User != null 
        && HttpContext.Current.User.Identity.Name != null)
{
    userName = HttpContext.Current.User.Identity.Name;
    userId = HttpContext.Current.User.Identity.GetUserId();
}
或者根据Darrel Miller的评论,可以先用它来检索HttpContext

// get httpContext
object httpContext;
actionContext.Request.Properties.TryGetValue("MS_HttpContext", out httpContext);    
另见:


在WebApi 2中,您可以从ApicController上的方法中使用
RequestContext.Principal
您还可以使用上的属性访问主体

因此,以下两种说法基本相同:

string id;
id = User.Identity.GetUserId();
id = RequestContext.Principal.Identity.GetUserId();

提示位于Webapi2自动生成的帐户控制器中

将getter的此属性定义为

public string UserIdentity
        {
            get
            {
                var user = UserManager.FindByName(User.Identity.Name);
                return user;//user.Email
            }
        }
为了让UserManager(在WebApi2中)像Romans(读作AccountController)一样工作

public ApplicationUserManager用户管理器
{
获取{return HttpContext.Current.GetOwinContext().GetUserManager();}
}

这应该在IIS和自主机模式下兼容

Karan Bhandari的回答很好,但项目中添加的AccountController很可能是一个
Mvc.Controller
。要将其答案转换为在ApiController中使用,请将HttpContext.Current.GetOwinContext()更改为请求.GetOwinContext(),并确保已使用语句添加了以下2个

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;

如果您使用的是Asp.Identity UseManager,它会自动设置

RequestContext.Principal.Identity.GetUserId()

基于您在创建IdentityDbContext时使用的IdentityUser

如果您正在实现自定义用户表和owin令牌承载身份验证,请查看我的答案


希望它仍然有用。:)

以上建议对我都不管用。下面的人做了

HttpContext.Current.Request.LogonUserIdentity.Name
我想有各种各样的场景,这一个对我很有用。我的场景涉及AngularJS前端和WebAPI2后端应用程序,两者都在IIS下运行。我必须将两个应用程序都设置为在Windows身份验证下以独占方式运行


无需传递任何用户信息。浏览器和IIS交换登录的用户凭据,Web API可以按需访问用户凭据(我想是从IIS).

在.Net Core中,使用
User.Identity.Name
获取用户的名称声明。

不要使用HttpContext,因为它限制了您的托管选项,并使您的代码难以测试。如果您是自托管,或者OwinHttpListener hostedWe没有该属性,HttpContext对象将不在字典中。嗯。也许我们使用的是旧版本的WebApi。@ShaunLuttin好的,所以如果您使用的是WebApi 1,那么我相信ApiController上有Prinicpal属性。这只是访问Request.Properties集合的一个奇特包装。主体对象存储在该字典中。只是不要忘记使用Microsoft.AspNet.Identity@Darreller有没有办法从代码的其他部分(不在控制器内)获得相同的(比如请求上下文)。@AjayAradhya只有在您将其传递到那里时才有。以前对web框架的研究表明,使用静态属性访问请求上下文会导致各种体系结构问题。一开始它很方便,但从长远来看它会带来很多痛苦。我试图使用您在这里编写的内容,但是GetUserId()函数总是返回null。用户经过身份验证,我正在传递一个承载令牌,ApiController具有[Authorize]头GetUserId()函数仅在用户具有NameIdentifier声明时返回ID。有关如何解决此问题的示例,请参阅Oswaldo在此处的回答:请参阅此回答:添加声明执行技巧
User.Identity.Name
获取声明的值。它不是特定于Active Directory的。@内特谢谢你的评论,我已经修复了它
HttpContext.Current.Request.LogonUserIdentity.Name