Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
When i m trying to login by entering login Id and password and it is checking the ip Address because i want to allow user from selected IP only..And i have set my IIS .net trust level to Medium then its giving me following error:-

Request for the permission of type 'System.Net.NetworkInformation.NetworkInformationPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

and when i am setting trust level to full its working proper..but as per requirement the trust level should be Medium only..Can anyone please help me out..Thanks in advance


 protected void btnLogin_Click(object sender, EventArgs e)
{
            string LoginName = txtloginname.Text.Trim();
            string Password = txtpassword.Text.Trim();
            AdminLogin oLogin = new AdminLogin();
            oLogin.LoginName = LoginName;
            oLogin.Password = Password;
            oLogin.MachineIp = "";
            string sa;
            string strHostName = System.Net.Dns.GetHostName();
            sa = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
           
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                sa = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
            {
                sa = HttpContext.Current.Request.UserHostAddress;
                sa = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            string s = GetMAcAddress();
            
            oLogin.MachineIp = sa.ToString().Trim();

            DataSet ds = AdminLoginManager.CheckLoginInfo(oLogin, conn);

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        
                            SessionInfo.LoginId = ds.Tables[0].Rows[0]["Admin_Login_Id"].ToString().Trim();
                            SessionInfo.Login_UserName = ds.Tables[0].Rows[0]["Admin_Login_Name"].ToString().Trim();

                            SessionInfo.LoginName = ds.Tables[0].Rows[0]["FullName"].ToString().Trim();

                            SessionInfo.LoginType = ds.Tables[0].Rows[0]["UserType"].ToString().Trim();

                            if (ds.Tables[0].Rows[0]["PwdChange"].ToString() == "N")
                            {
                                Response.Redirect("/ChangePassword.aspx", false);
                            }
                            else
                            {
                                Response.Redirect("/Admin_Home.aspx", false);
                            }
                            Log logObj = new Log();
                            string servername = "CjOrder";
                            string username = txtloginname.Text.Trim();
                            string eventtype = logObj.adUserValid;
                            string eventdesc = "User Logged In Successfully";
                            LogManager.Insert_Log(conn, servername, username, eventtype, eventdesc, oLogin.MachineIp);
                       
                    }
                    
          }     

private string GetMAcAddress()
    {
        string macAddress = string.Empty;
        foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
            {
                macAddress += nic.GetPhysicalAddress().ToString();
                break;
            }
        }
        return macAddress;
}
Posted

1 solution

1) You can determine the exact permission requirement with permcalc[^].
2) I am not really on top with all permission requirements, but you want to do some low level stuff. I can imagine, that you need registryaccess permission to get mac address of the interface. With medium trust you won't have.
3) You can keep the medium trust level, but define custom policy to have access to what your application really needs: https://msdn.microsoft.com/en-us/library/ff648344.aspx[^]
4) What do you inted to do with local machine's MAC addresses all concatenated? First: use stringbuilder to concatenate, secound: you might have more than ten active interface MACs on a physical server.
5) IIS has built in IP access security: https://www.iis.net/configreference/system.webserver/security/ipsecurity[^]

Please clarify what your concrete intentions are...
 
Share this answer
 
v2
Comments
Member 11864926 17-Oct-15 7:50am    
The requierement is that we take order for some product. and i want person of store can only place order from his store only and dont have login permission from any other network..
Zoltán Zörgő 17-Oct-15 8:00am    
OK.
- what's the problem with IIS IP security?
- why the MAC?
Member 11864926 19-Oct-15 3:13am    
Could you please tell me if i want ip security, do i have to get the MAC id for that or i can just get the Ip Address and make the system secure..
Zoltán Zörgő 19-Oct-15 3:41am    
Please note that MAC addresses are of no use in your case. Any MAC can be changed on any client these days. On the other hand, the MAC address works on layer 2. This means that if the client and the server are located on other LAN or VLAN, they do not have any clue about the MAC of the other party.
But IP security is neither a good solution. You might encounter address changes anytime. Just think about DHCP, which is common.
I suggest you investiagte a little bit the possibility of certificate based authentication. See: https://www.iis.net/configreference/system.webserver/security/authentication/clientcertificatemappingauthentication
Member 11864926 19-Oct-15 5:21am    
thank you so much for the help..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900