Owin auth - 如何获取请求身份验证令牌的客户端的IP地址

Ser*_*e P 13 .net c# authentication owin

使用Owin Security,我试图让API有2种身份验证方法.

context变量(OAuthGrantResourceOwnerCredentialsContext)中是否有一个属性可以让我访问客户端IP地址,向API发送auth令牌的初始请求?

我的auth方法的基本条带如下所示:

public override async Task GrantResourceOwnerCredentials(
    OAuthGrantResourceOwnerCredentialsContext context)
{
    await Task.Run(() =>
    {
        var remoteIpAddresss = context.Request.RemoteIpAddress;
        var localIpAddress = context.Request.LocalIpAddress;


        // ... authenticate process goes here (AddClaim, etc.)
    }
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解remoteIpAddresslocalIpAddressAPI(即API的托管位置).我如何知道请求从哪个IP地址(和端口)发送?

客户是否需要自己发送此信息?

我应该在auth路径中添加额外的参数吗?(除了典型username,password,grant_type)?

Ser*_*e P 20

所以,要回答我自己的问题,请纠正我,如果我错了,但是:

var remoteIpAddresss = context.Request.RemoteIpAddress;
Run Code Online (Sandbox Code Playgroud)

客户端的IP地址(请求身份验证令牌的用户),以及

var localIpAddress = context.Request.LocalIpAddress;
Run Code Online (Sandbox Code Playgroud)

Web Api的IP地址(托管API的地方).

  • 我也想知道这一点,所以我在VM(具有不同的IP)上设置了一个项目,从主机发送了一个请求,你确实是正确的.`context.Request.RemoteIpAddress`显示了请求令牌的客户端的IP,`context.Request.LocalIpAddress`显示了托管API的IP. (2认同)