get remote ip address, referrer url of signalr client in signalr cross-domain

Jacob
Jacob
Member
56 Points
13 Posts

Hi,

I am creating api of live currency rate in signalr cross-domain. I want give signalr javascript client to the client.

So I want to validate with ip address or referrer url in which client will add the javascript client for signaling.

how to get remote ip address or referrer url of signalr client in signalr cross-domain

Views: 14260
Total Answered: 3
Total Marked As Answer: 1
Posted On: 12-Sep-2015 02:13

Share:   fb twitter linkedin
Answers
Brian
Brian
Moderator
2232 Points
14 Posts
         

Hi Jacob,

You need to extend the context method to get httpcontext. For this you need to add following class file:

using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ExtensionClass
/// </summary>
public static class ExtensionClass
{
/// <summary>
/// Returns the <see cref="HttpContextBase"/> for this <see cref="IRequest"/>.
/// </summary>
/// <param name="request">The request</param>
public static HttpContextBase GetHttpContext(this IRequest request)
{
object httpContextBaseValue;
if (request.Environment.TryGetValue(typeof(HttpContextBase).FullName, out httpContextBaseValue))
{
return httpContextBaseValue as HttpContextBase;
}
return null;
}
public static string GetRemoteIpAddress(this IRequest request)
{
object ipAddress;
//server.LocalIpAddress
if (request.Environment.TryGetValue("server.RemoteIpAddress", out ipAddress))
{
return ipAddress as string;
}
return "";
}
}

 

And then you can get remote ip address and referrer url on connected as :

public override Task OnConnected()
{
string ClientReferrerURL = Context.Request.GetHttpContext().Request.ServerVariables["HTTP_REFERER"];
string RemoteIpAddress = Context.Request.GetRemoteIpAddress();
 
return base.OnConnected();
}

 

Posted On: 12-Sep-2015 02:38
Jacob
Jacob
Member
56 Points
13 Posts
         

Hi,

It's really helpful. Thaks...

Posted On: 25-Nov-2015 10:44
Rashmi
Rashmi
Member
820 Points
17 Posts
         

Old post but really helpful.

Posted On: 17-Apr-2024 03:50
 Log In to Chat