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

Jacob
Jacob
34 Points
17 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: 8503
Total Answered: 2
Total Marked As Answer: 1
Posted On: 12-Sep-2015 02:13

Share:   fb twitter linkedin
Answers
Brian
Brian
Participant
536 Points
73 Posts
Marked as answer      

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
34 Points
17 Posts
     

Hi,

It's really helpful. Thaks...

Posted On: 25-Nov-2015 10:44
banner

Blog

Active User (2)

 Log In to Chat