获取当前用户,ApiController操作中,没有通过用户ID作为参数 [英] Get the current user, within an ApiController action, without passing the userID as a parameter

查看:112
本文介绍了获取当前用户,ApiController操作中,没有通过用户ID作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何获取当前用户,安全ApiController操作中,没有通过用户名或用户ID作为一个参数?

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

我们假设这是可用的,因为我们是一个安全的操作中。作为在一个安全的行动意味着用户已经通过身份验证和要求有她承载令牌。鉴于的WebAPI已授权用户,可能有一个内置的方式来访问用户id,而不必把它作为一个动作参数

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.

推荐答案

在2的WebAPI你可以使用 RequestContext.Principal 从ApiController在方法中

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

这篇关于获取当前用户,ApiController操作中,没有通过用户ID作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆