本文整理汇总了C#中System.Net.Http.HttpRequestMessage.GetClientIpAddress方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.GetClientIpAddress方法的具体用法?C# HttpRequestMessage.GetClientIpAddress怎么用?C# HttpRequestMessage.GetClientIpAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.GetClientIpAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WebApiRaygunRequestMessage
public WebApiRaygunRequestMessage(HttpRequestMessage request)
{
HostName = request.RequestUri.Host;
Url = request.RequestUri.AbsolutePath;
HttpMethod = request.Method.ToString();
Headers = new Dictionary<string, string>();
foreach (var header in request.Headers)
{
Headers.Add(header.Key, string.Join(",", header.Value));
}
IPAddress = request.GetClientIpAddress();
if (request.Content.Headers.ContentLength.HasValue && request.Content.Headers.ContentLength.Value > 0)
{
foreach (var header in request.Content.Headers)
{
Headers.Add(header.Key, string.Join(",", header.Value));
}
try
{
RawData = request.Content.ReadAsStringAsync().Result;
}
catch (Exception)
{
}
}
}
示例2: LogRequestLoggingInfo
private void LogRequestLoggingInfo(HttpRequestMessage request)
{
var info = new ApiLoggingInfo
{
HttpMethod = request.Method.Method,
UriAccessed = request.RequestUri.AbsoluteUri,
IpAddress = request.GetClientIpAddress(),
MessageType = HttpMessageType.Request,
LoggingLevel = LoggingLevel.Trace
};
ExtractMessageHeadersIntoLoggingInfo(info, request.Headers.ToList());
if (request.Content != null)
{
request.Content.ReadAsByteArrayAsync()
.ContinueWith(task =>
{
info.BodyContent = Encoding.UTF8.GetString(task.Result);
_repository.Log(info);
});
return;
}
_repository.Log(info);
}
示例3: GetClientIpAddressForEmptyXForwardedForReturnsNull
public void GetClientIpAddressForEmptyXForwardedForReturnsNull()
{
var sut = new HttpRequestMessage();
sut.Headers.Add(ForwardedFor, string.Empty);
Assert.Equal(null, sut.GetClientIpAddress());
}
示例4: GetClientIpAddressForPartiallyMalformedXForwardedForReturnsProperlyFormedClientIp
public void GetClientIpAddressForPartiallyMalformedXForwardedForReturnsProperlyFormedClientIp()
{
var sut = new HttpRequestMessage();
sut.Headers.Add(ForwardedFor, MalformedIpAddress);
sut.Headers.Add(ForwardedFor, GoogleIpAddress);
sut.Headers.Add(ForwardedFor, MalformedIpAddress);
Assert.Equal(GoogleIpAddress, sut.GetClientIpAddress());
}
示例5: GetClientIpAddressForPublicClientAndMultipleProxiesReturnsPublicClientIp
public void GetClientIpAddressForPublicClientAndMultipleProxiesReturnsPublicClientIp()
{
var sut = new HttpRequestMessage();
sut.Headers.Add(ForwardedFor, MicrosoftIpAddress);
sut.Headers.Add(ForwardedFor, Private16Bit);
sut.Headers.Add(ForwardedFor, Private20Bit);
sut.Headers.Add(ForwardedFor, Private24Bit);
sut.Headers.Add(ForwardedFor, PrivateLinkLocal);
Assert.Equal(MicrosoftIpAddress, sut.GetClientIpAddress());
}
示例6: GetUserIdentifier
protected virtual string GetUserIdentifier(HttpRequestMessage request) {
HttpRequestContext ctx = request.GetRequestContext();
if (ctx != null) {
// use organization id or user id as the identifier for throttling
var principal = request.GetRequestContext().Principal as ExceptionlessPrincipal;
if (principal != null)
return principal.Project != null ? principal.Project.OrganizationId : principal.UserEntity.Id;
}
// fallback to using the IP address
var ip = request.GetClientIpAddress();
return Settings.Current.WebsiteMode == WebsiteMode.Dev && String.IsNullOrEmpty(ip) ? "127.0.0.1" : ip;
}
示例7: GetUserIdentifier
protected virtual string GetUserIdentifier(HttpRequestMessage request) {
HttpRequestContext ctx = request.GetRequestContext();
if (ctx != null) {
// use organization id or user id as the identifier for throttling
var principal = request.GetRequestContext().Principal as ExceptionlessPrincipal;
if (principal != null)
return principal.Project != null ? principal.Project.OrganizationId : principal.UserEntity.Id;
}
// fallback to using the IP address
return request.GetClientIpAddress();
}
示例8: TestGetClientIpAddressFromRemoteEndpointMessage
public void TestGetClientIpAddressFromRemoteEndpointMessage()
{
var remoteEndpointMessageProperty = new RemoteEndpointMessageProperty("127.0.0.1", 8050);
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Properties.Add(
"System.ServiceModel.Channels.RemoteEndpointMessageProperty",
remoteEndpointMessageProperty);
var result = httpRequestMessage.GetClientIpAddress();
Assert.That(result, Is.EqualTo("127.0.0.1"));
}
示例9: TestGetClientIpAddressFromOwinContext
public void TestGetClientIpAddressFromOwinContext()
{
var owinContextStub = MockRepository.GenerateMock<OwinContext>();
var owinRequest = new OwinRequest { RemoteIpAddress = "127.0.0.1" };
owinContextStub.Stub(x => x.Request).Return(owinRequest).Repeat.Once();
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Properties.Add("MS_OwinContext", owinContextStub);
var result = httpRequestMessage.GetClientIpAddress();
Assert.That(result, Is.EqualTo("127.0.0.1"));
}
示例10: SendAsync
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var corrId = string.Format("{0}{1}", DateTime.Now.Ticks, Thread.CurrentThread.ManagedThreadId);
var requestInfo = string.Format("{0} {1}", request.Method, request.RequestUri);
var _CurrentDateTime = DateTime.Now;
var _CurrentIpAddress = request.GetClientIpAddress();
var requestMessage = await request.Content.ReadAsByteArrayAsync();
var reqHeaders = "";
var header = request.Headers;
if (header != null)
{
//for (int i = 0; i < header.Count(); i++)
// request += header
var _aaa = header.ToArray();
for (int i = 0; i < _aaa.Count(); i++)
reqHeaders += _aaa[i].Key + ":" + _aaa[i].Value.FirstOrDefault() + ", ";
}
var httpStatusCode = "";
byte[] requestBody = null;
if (request.Content != null)
requestBody = await request.Content.ReadAsByteArrayAsync();
await IncommingMessageAsync(_CurrentIpAddress, _CurrentDateTime, corrId, requestInfo, requestMessage, reqHeaders, httpStatusCode, requestBody);
var response = await base.SendAsync(request, cancellationToken);
httpStatusCode = (int)response.StatusCode + " " + response.StatusCode.ToString();
byte[] responseMessage;
//if (response.IsSuccessStatusCode)
// responseMessage = await response.Content.ReadAsByteArrayAsync();
//else
byte[] _responseMessageMain = null;
if (response.Content != null)
_responseMessageMain = await response.Content.ReadAsByteArrayAsync();
responseMessage = Encoding.UTF8.GetBytes(response.ReasonPhrase);
await OutgoingMessageAsync(_CurrentIpAddress, _CurrentDateTime, corrId, requestInfo, responseMessage, reqHeaders, httpStatusCode, requestBody, _responseMessageMain);
return response;
}
示例11: TestGetClientIpAddressFromHttpContext
public void TestGetClientIpAddressFromHttpContext()
{
var httpContextBaseStub = MockRepository.GenerateMock<HttpContextBase>();
var httpRequestBaseStub = MockRepository.GenerateMock<HttpRequestBase>();
httpRequestBaseStub.Stub(x => x.UserHostAddress).Return("127.0.0.1").Repeat.Once();
httpContextBaseStub.Stub(x => x.Request).Return(httpRequestBaseStub).Repeat.Once();
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Properties.Add("MS_HttpContext", httpContextBaseStub);
var result = httpRequestMessage.GetClientIpAddress();
Assert.That(result, Is.EqualTo("127.0.0.1"));
httpContextBaseStub.VerifyAllExpectations();
httpRequestBaseStub.VerifyAllExpectations();
}
示例12: GetUserIdentifier
protected virtual string GetUserIdentifier(HttpRequestMessage request) {
var authType = request.GetAuthType();
if (authType == AuthType.Token)
return request.GetDefaultOrganizationId();
if (authType == AuthType.User) {
var user = request.GetUser();
if (user != null)
return user.Id;
}
// fallback to using the IP address
var ip = request.GetClientIpAddress();
if (String.IsNullOrEmpty(ip) || ip == "::1")
ip = "127.0.0.1";
return ip;
}
示例13: GetClientIpAddressForNullXForwardedForReturnsNull
public void GetClientIpAddressForNullXForwardedForReturnsNull()
{
var sut = new HttpRequestMessage();
Assert.Equal(null, sut.GetClientIpAddress());
}
示例14: GetUserIdentifier
protected virtual string GetUserIdentifier(HttpRequestMessage request)
{
return request.GetClientIpAddress();
}
示例15: GetUserIdentifier
//override added to support OWIN, as at the time of writing, ThrottlingHandler did not support OWIN host
protected override string GetUserIdentifier(HttpRequestMessage request)
{
var id = request.GetClientIpAddress();
return id;
}