Error using methods to generate hash and paste MAC address

-1

According to this my question , in the Cigano's response to the hash of the mac address of the network card, just use two methods.

But how to use them to retrieve the data and do the verification I'm having an error:

  

A field initializer can not reference that non-static field, method, or propety

Here's how to use it:        var chave = GetSHA1HashData(GetMacAddress());

Can anyone help me?

Here are the methods in question to facilitate:

  public class AutenticacaoController : Controller
    {
    private EntidadesContexto db;
    string chave = GetSHA1HashData(GetMacAddress());

    public AutenticacaoController()
    {
        db = new EntidadesContexto();
    }
    protected override void Dispose(bool disposing)
    {
        if (db != null) db.Dispose();
        base.Dispose(disposing);
    }
    // GET: Autenticacao
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Index(String Login, String Senha)
    {
        //verificando login pelo usuario do banco de dados ...
        Usuario login = db.Usuarios.Where(x => x.Login == Login && x.Senha == Senha).FirstOrDefault();
        if (login != null)
        {
            FormsAuthentication.SetAuthCookie(login.Nome.ToString(), false);
            Session.Add(".PermissionCookie", login.Perfil);
            Session.Add("UsuarioID", login.UsuarioID);
            return RedirectToAction("Index", "Home"); //pagina padrao para todos os usuarios...
        }
        return RedirectToAction("Index");

    }
    public ActionResult Sair()
    {

        FormsAuthentication.SignOut();
        Session.Remove(".PermissionCookie");
        return RedirectToAction("Index");
    }

    //Aqui são métodos para pegar o endereço MAC da placa de rede do computador e a considerá-la como chave de licença
    private string GetSHA1HashData(string data)
    {
        SHA1 sha1 = SHA1.Create();

        byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));

        StringBuilder returnValue = new StringBuilder();

        for (int i = 0; i < hashData.Length; i++)
        {
            returnValue.Append(hashData[i].ToString());
        }

        return returnValue.ToString();
    }

    private string GetMacAddress()
    {
        string macAddresses = string.Empty;

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                break;
            }
        }

        return macAddresses;
    }
    //Aqui termina
    
asked by anonymous 25.11.2014 / 23:23

2 answers

1

It will not work the way it is. You can not create a variable initialized in the class populated by methods of the same class. In practice, it's as if the compiler does not know the methods.

There are two ways to solve:

1. Calling Methods Within Another Method

[HttpPost]
public ActionResult Index(String Login, String Senha)
{
    string chave = GetSHA1HashData(GetMacAddress());

    ...
}

2. Encapsulating Methods in Static Classes

public static class GeradorDeLicencas
{
    public static string GetSHA1HashData(string data) { ... }
    public static string GetMacAddress() { ... }
}
    
25.11.2014 / 23:50
3

Cause

The key variable is declared at the class level, and therefore is a field. Fields can not be initialized (declared and defined at the same time) with a non-static value, method, or property. You need to declare it at the class level, and set it in the constructor.

Why?

A field can only be initialized with static members, since the instance is not defined until the constructor is executed, and direct initialization happens before any constructor is executed.

    
25.11.2014 / 23:49