Skip to content

Ip Limitation In SignalGo

Gerardo Sista edited this page Feb 15, 2019 · 9 revisions

You can limit client IP addresses server side. Just use ClientLimitation attribute directly on methods you want to limit Examples: in this example clients with addresses "123.123.162.169" and "321.528.123.144" are authorized to call your "RemoveTagInfo" method:

        [SignalGo.Server.DataTypes.ClientLimitation(AllowAccessList = new string[] { "123.123.162.169" , "321.528.123.144" })]
        public MessageContract RemoveTagInfo(int tagId)
        {
            //code logic
            return null;
        }

in this example the client with "2.2.162.169" ip address is not authorized to call "RemoveTagInfo" method:

        [SignalGo.Server.DataTypes.ClientLimitation(DenyAccessList = new string[] { "2.2.162.169" })]
        public MessageContract RemoveTagInfo(int tagId)
        {
            //code logic
            return null;
        }

You can do also custom attributes for limitations like in this example:

    public class MyClientLimitationAttribute : SignalGo.Server.DataTypes.ClientLimitationAttribute
    {
        /// <summary>
        /// returns clients you want authorize
        /// </summary>
        /// <returns></returns>
        public override string[] GetAllowAccessIpAddresses()
        {
            //code logic to authorize ip addresses
            //if userid == 1 only 125.235.211.25 ip can call this method, all others cannot call the method
            if (SignalGo.Server.Models.OperationContext<MyCurrentUser>.UserId == 1)
                return new string[] { "125.235.211.25" };
            else
                return null;
        }

        /// <summary>
        /// return every client you don't want to authorize
        /// </summary>
        /// <returns></returns>
        public override string[] GetDenyAccessIpAddresses()
        {
            //code logic to deny ip addresses
            //if userid == 1 then "125.235.211.25" IP can't call this method. All others can call the method
            if (SignalGo.Server.Models.OperationContext<MyCurrentUser>.UserId == 2)
                return new string[] { "125.235.211.25" };
            else
                return null;
        }
    }

to use it just use MyClientLimitationAttribute on your methods like here:

        [MyClientLimitation]
        public MessageContract RemoveTagInfo(int tagId)
        {
            //code logic
            return null;
        }

Please note that I wrote only MyClientLimitation and not MyClientLimitationAttribute following C# convention!