允许专家 IP 地址使用“我的 Windows 窗体”应用程序 C#



我有一个Windows表单应用程序,我只想允许IP地址列表打开连接并使用我的应用程序我像那样编辑我的应用程序.config

    <?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>

但它根本不起作用,现在当我运行我的应用程序时抛出错误,我收到此错误"System.Data.SqlClient.SqlConnection"的类型初始值设定项引发了异常。我的代码是:-

 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();

我认为你不能通过配置文件做到这一点。

我建议您在加载应用程序之前执行此操作。在那里,您只需要获取用户的当前IP地址。如果他不被允许,你可以说

Environment.Exit(0);

完整代码示例:

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!");
    }
}

来自 IP 查找的代码是根据 https://stackoverflow.com/a/6803109/4198052。

我希望我能帮助你。

最新更新