How to login user in WebForms remotely C #,


Hi, I have a question about logging into web forms, my login works perfectly this way:


And when clicking, it calls a class that validates and returns true or false, according to the credentials sent and consulted in the "Users" database Login.cs:

 protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        bool Autenticado = false;
        cls_login validacion = new cls_login();
        Autenticado = validacion.validar(Login1.UserName, Login1.Password);
        if (Autenticado) {

            e.Authenticated = Autenticado;

The problem is that I'm doing an extension for Chrome, where the Popup is open iframe to the web page of my WebForms application, where obviously opens the login page, where they log in and everything works perfectly .

The annoying thing is that I would like that every time they open the extension they avoid having to log in.

I would not like the webforms application to have the session never expire, but rather my solution was that in the options of my extension I saved the username and password and using javascript ajax , I will call a < strong> WebHandler where it receives the credentials and will log in, but I can not do it since I do not have the same events as in my login.cs:

   protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)

To date I have been able to call my WebHandler using JS (from the chrome extension), it has received the parameters correctly, it has called my validation class that is in turn queries the base of data in SQL, and the Handler returns true or false, which likewise works correctly.

My question is how can I generate a "login" in a class?

asked by Carlos Miranda 05.05.2016 в 20:29

1 answer


I could solve it, a login can not be created in a class in App_Code, so create a WebMethod that receives a username and password from an html with javascript (ajax)

The code is as follows:

public partial class service_login_text : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    public static string setLogin(string usuario, string password)
        if (usuario != null && password != null) { //Comprobamos que los datos recibidos desde ajax no sean nulos
            try {
                bool login = false; // Establecemos login en falso hasta que se demeustre lo contrario
                cls_login validacion = new cls_login(); // Instanciamos nuestra clase que validara el login en una BD
                login = validacion.validar(usuario, password); // Llamanos a nuestra función y esta devolvera un true o false

                if (login) { // Si fue correcto creamos la autentificación
                    service_login_text autentificacion = new service_login_text(); // Creamos nuestra instancia de esta misma clase
                    object sender = new object(); // Objeto generico
                    autentificacion.Login1_Authenticate(sender, new AuthenticateEventArgs(), usuario); // Parametros de envio
                    return "Login correcto";
                else { return "Login Incorrecto"; };
            catch(Exception ex) {

                return "Error Catch";
        } else{   
            return "No se ha recibido información";

    // La clase de autentificación esta en en el mismo WebMethod por que permite crear las variables "Session" lo cual es imposible crear
    // en una clase ubicada en App_Code por que el nivel de protección no lo permite.
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e, string usuario){
        HttpContext.Current.Session["UserName"] = usuario; //El truco esta en crear la variable "UserName" con un usuario válido y "IsAuthenticated" en true
        HttpContext.Current.Session["IsAuthenticated"] = true;

        FormsAuthentication.SetAuthCookie(usuario, true); // Creamos una cookie de autentificación
        e.Authenticated = true; // El evento  AuthenticateEventArgs en true
answered by 06.05.2016 / 16:26