操作中获取当前用户

操作中获取当前用户

本文介绍了在 ApiController 操作中获取当前用户,而不将 userID 作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在安全的 ApiController 操作中获取当前用户,而不将 userName 或 userId 作为参数传递?

How do we get the current user, within an secure ApiController action, without passing the userName or userId as a parameter?

我们假设这是可用的,因为我们处于安全操作范围内.处于安全操作中意味着用户已经通过身份验证并且请求具有她的不记名令牌.鉴于 WebApi 已授权用户,可能有一种内置的方式来访问 userId,而不必将其作为操作参数传递.

We assume that this is available, because we are within a secure action. Being in a secure action means that the user has already authenticated and the request has her bearer token. Given that WebApi has authorized the user, there may be a built in way to access the userId, without having to pass it as an action parameter.

推荐答案

在 WebApi 2 中,您可以在 ApiController 上的方法中使用 RequestContext.Principal

In WebApi 2 you can use RequestContext.Principal from within a method on ApiController

这篇关于在 ApiController 操作中获取当前用户,而不将 userID 作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 20:54