using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Regex.IsMatch(txtUsuario.Text.Trim(), @"^[0-9a-zA-Z_\s\.\-]{1,50}$") &&
        Regex.IsMatch(txtSenha.Text.Trim(), @"^[0-9a-zA-Z_\s\.\-]{1,50}$"))
        {

            if (Membership.ValidateUser(txtUsuario.Text.Trim(), txtSenha.Text.Trim()))
            {
                string dadosUser = "Olá usuário.";
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1,
                    txtUsuario.Text.Trim(),
                    DateTime.Now,
                    DateTime.Now.AddMinutes(10),
                    false,
                    dadosUser,
                    FormsAuthentication.FormsCookiePath);
                string cook = FormsAuthentication.Encrypt(ticket);
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, cook));
            }
            else
            {
                Response.Write("Login Inválido.");
            }
        }
        else
        {
            Response.Write("Insira um nome de usuário ou senha válidos.");
        }
    }
}