Ramco Ramco

Ramco Ramco

  • 424
  • 3.4k
  • 491.3k

Authenticate User

Aug 20 2024 6:26 AM

Hi

  In Authenticate User it is going in else part. 

Login Page

using (SqlConnection con = new SqlConnection(Common.CommonFunction.cnn_Live))
{
    string UserId = txtUserId.Text;
    string Pwd = Common.CommonFunction.EncodePasswordToBase64(txtPassword.Text.ToUpper());
    SqlCommand cmd = new SqlCommand("sp_Login", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@UserId", UserId);
    cmd.Parameters.AddWithValue("@Password", Pwd);
    SqlParameter successParam = cmd.Parameters.Add("@Success", SqlDbType.Bit);
    successParam.Direction = ParameterDirection.Output;
    con.Open();

    cmd.ExecuteNonQuery();
    bool success = (bool)successParam.Value;
    if (success)
    {
        Response.Redirect("~/Admin/Department.aspx");
    }
    else
    {
        //ShowMessage("Oops...", success.ToString(), "error");
    }
}

Department Page
if (!Page.IsPostBack)
{
    var authenticatedUser = Common.CommonFunction.AuthenticatedUser();

    if (authenticatedUser.Item1) // Check if the user is authenticated
    {
        var userRole = authenticatedUser.Item2;
        var loginId = authenticatedUser.Item3;
    }
    else
    {
        // Handle unauthenticated scenario if needed
    }
}


public static Tuple<Boolean, String, String> AuthenticatedUser()
{
    try
    {

        if (HtpContext.Current.User.Identity.IsAuthenticated) //Check if user is loged in or not
        {
            FormsIdentity id = (FormsIdentity)HtpContext.Current.User.Identity;
            FormsAuthenticationTicket ticket = id.Ticket;
            return Tuple.Create(true, ticket.UserData, Convert.ToString(HtpContext.Current.User.Identity.Name));
        }
        else
        {
            FormsAuthentication.RedirectToLoginPage();
            return Tuple.Create(false, "", "");
        }
    }
    catch (Exception ex)
    {
        FormsAuthentication.RedirectToLoginPage();
        return Tuple.Create(false, "", "");
    }
}

Thanks


Answers (3)