Serenity etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Serenity etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

BlackListIP control on Serenity platform (.NET Core)

 In the Serenity platform, if you want to block IPs that belong to people you do not want to come from outside in the .net core web project, you can follow the steps below.


1- First, create the IP definition script where you can enter the IP information of the person/company you want to block.

CREATE TABLE [dbo].[BlackList](

[BlackListId] [int] IDENTITY(1,1) NOT NULL,

[IpNumber] [varchar](100) NOT NULL,

[Description] [varchar](100) NULL,

 CONSTRAINT [PK_BlackList] PRIMARY KEY CLUSTERED 

(

[BlackListId] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

GO



2- Then generate the Row, Controller, Enpoint and TypeScript codes for the BlackList table via Serenity.




3- Add the class that allows the detection of the external IP to the project. GetIPNumberService.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Net.Http;

using System.Threading.Tasks;


namespace FercamB2B.Web.Modules.Common.Helpers

{

    public class GetIPNumberService

    {

        private readonly HttpClient _httpClient;


        public GetIPNumberService(HttpClient httpClient)

        {

            _httpClient = httpClient;

        }

        public async Task<string> GetUserIpAsync()

        {

            try

            {

                var response = await _httpClient.GetStringAsync("https://ipv4.icanhazip.com");

                return response.Trim();

            }

            catch

            {

                return null; // IP alınamazsa null döner

            }

        }

        private class IpInfo

        {

            public string Ip { get; set; }

        }

    }

}


4- Add the following method to EndPoint.

   

        [HttpPost]

        public BlackListResponse CheckIfBlacklisted(IDbConnection connection)

        {

            BlackListResponse blackListResponse = new BlackListResponse();

            // Senkron IP alma metodunu kullanın

            var ipAddress = _getIPNumberService.GetUserIpAsync().GetAwaiter().GetResult();


            if (ipAddress == null)

            {

                blackListResponse.Durum = false;

                return blackListResponse; // IP alınamazsa, blacklistte değildir

            }


            var blacklistedIp = connection.TryFirst<MyRow>(q => q

                .SelectTableFields()

                .Where(new Criteria(MyRow.Fields.IpNumber) == ipAddress));

            if(blacklistedIp!=null)

            {

                blackListResponse.Durum = true;

                blackListResponse.IP = ipAddress;

            }

            else

            {

                blackListResponse.Durum = false;

            }


            return blackListResponse;

        }

5- Add the IP control method to Loginpanel.ts.

 this.byId('LoginButton').click(e => {

                e.preventDefault();


                if (!this.validateForm()) {

                    return;

                }

               

                let ipAdresi = "";

                let blacklist = false;

                Parametreler.BlackListService.CheckIfBlacklisted({


                }, response4 => {

                    if (response4.Durum == true) {

                        Q.alert("Your IP address is blacklisted!");

                        blacklist = true;

                        return;

                    }

                    ipAdresi = response4.IP;


                }, { async: false });

                if (blacklist) {

                    return;

                }

                console.log("IP address used for login: " + ipAdresi);

As a result, the system that provides control of the entered IP works healthily in this way.



BlackListIP control on Serenity platform (.NET Core)

 In the Serenity platform, if you want to block IPs that belong to people you do not want to come from outside in the .net core web project,...