本文整理匯總了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;
}