-1

i have a windows form application i want to allow only IP Address list to open the connection and use my application i editing my app.config like that

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<security>
<ipSecurity allowUnlisted="false">              
    <clear/> 
    <add ipAddress="187.20.6.4" allowed="true"/>   
    <add ipAddress="192.8.0.6" allowed="true"/>
    <add ipAddress="172.24.0.4" allowed="true"/>
    <add ipAddress="172.29.16.138" allowed="true"/>
    <add ipAddress="172.23.30.82" allowed="true"/>
    <add ipAddress="10.0.2.15" allowed="true"/>   
    </ipSecurity>
    </security>
    <connectionStrings>
    <add name="con" connectionString="Data Source=serverip;Initial          Catalog=mydatabase;Persist Security Info=True;User ID=user;Password=pass"
        providerName="System.Data.SqlClient" />
      </connectionStrings>
    </configuration>

but it didn't work at all it now throw an error when i run my application and i get this error The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. my code is :-

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
 using System.Text;
 using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace myprogram
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
        SqlConnection con= new SqlConnection("Data Source=serverip;Initial 
       Catalog=mydatabase;Persist Security Info=True;User    
            ID=username;Password=mypassword;
    SqlCommand command = new SqlCommand();
    SqlDataReader DataSearch;
     DataTable Dt = new DataTable();


    private void Eve_Ins()
    {
        int val;
        if (int.TryParse(textbox.Text, out val))
        {
            if (int.TryParse(textbox2.Text, out val))
            {
                if (textbox3.Text != "")
                {
                    con.Open();
                    if (!string.IsNullOrEmpty(textbox4.Text))
                    {
                        command.CommandText = "my sql command";
                        int i = command.ExecuteNonQuery();
                        con.Close();
Ahmed
  • 26
  • 5

1 Answers1

0

I don't think you can do this via the config-file.

I suggest you are doing this before the application is loaded. There you just need to grab the current ip address of the user. If he isn't allowed, you can just say

Environment.Exit(0);

Full code example:

static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // check if network is connected
        if (!NetworkInterface.GetIsNetworkAvailable() || CurrentIpRestricted(GetLocalIPAddress()))
        {
            // Exit Code Access Denied
            Environment.Exit(5);
        }

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    // you need to think about, if you want a WhiteList or a Blacklist and configure the code
    private static HashSet<string> Whitelist = new HashSet<string>();
    private static bool CurrentIpRestricted(string local)
    {
        return !Whitelist.Contains(local);
    }

    public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        throw new Exception("Local IP Address Not Found!");
    }
}

The code from the IP Lookup is according to https://stackoverflow.com/a/6803109/4198052.

I hope I could help you.

Community
  • 1
  • 1
Marius
  • 562
  • 5
  • 26