locked
how to make a login/password authentication in asp.net mobile ? RRS feed

  • Question

  • User1752237686 posted
    how to make a login/password authentication in asp.net mobile ?

    thx in advance

    Tuesday, August 14, 2007 1:08 AM

Answers

  • User1565039490 posted

    Creating of a login/password authentification in asp.net mobile is in many ways similar to web. Try script below:

    using System.Configuration;
    using System.Data.SqlClient;
    using System.Web.Security; 

    protected void Page_Load(object sender, EventArgs e) {

            // Assign properties to SelectionList mobile control

              chkPersist.Items.Add("Click here to save Login Session");
              chkPersist.Items[0].Value =
    "save";
    }

    public static int LoginUser(string username, string password) {

             // Create Instance of your site Connection and Command Object
             // You need to define Connection object in the web.config file

            SqlConnection conPortal = new SqlConnection(ConfigurationManager.ConnectionStrings["Portal"].ConnectionString);
            SqlCommand cmdLoginUser = new SqlCommand("Portal_LoginUser", conPortal);

            // Mark the Command as a SPROC
            cmdLoginUser.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            cmdLoginUser.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmdLoginUser.Parameters.Add(
    "@username", username);
            cmdLoginUser.Parameters.Add(
    "@password", password);

            // Execute the command
            conPortal.Open();
           
            cmdLoginUser.ExecuteNonQuery();
           
    int retVal = (int)(cmdLoginUser.Parameters["@RETURN_VALUE"].Value);
           
            conPortal.Close();

            return retVal;
    }

    protected void cmdLogin_Click(object sender, EventArgs e) {

            bool blPersist = false;

            // Determine whether password should be persisted
            if (chkPersist != null)
                blPersist = chkPersist.Items[0].Selected;

           // Either login or display error message
            switch (LoginUser(txtUsername.Text, txtPassword.Text)) {
                case 0: // Success!
                   
    FormsAuthentication.SetAuthCookie(txtUsername.Text, blPersist);
                   
    string redirectUrl = FormsAuthentication.GetRedirectUrl(txtUsername.Text, blPersist).ToLower();

                    if (redirectUrl.IndexOf("users_logout.aspx") == -1) {
                        if (Context.Request.QueryString["_lang"] != null) {
                             Context.Response.Redirect(redirectUrl);
                         }
                        else {
                            Context.Response.Redirect(redirectUrl);
                        }
                    }
                   else {
                      if (Context.Request.QueryString["_lang"] != null) {
                            Context.Response.Redirect(
    "default.aspx?_lang=" + Context.Request.QueryString["_lang"].ToString());
                      }
           
              else {
                           Context.Response.Redirect(
    "default.aspx?_lang=en");
                      }
                   }
                break;
                case 1: // Invalid Password
                    pnlInvalidPassword.Visible = true;
                    pnlInvalidUsername.Visible =
    false;
                break;
                case 2: // Invalid Username
                    pnlInvalidUsername.Visible = true;
                    pnlInvalidPassword.Visible =
    false;
                break;
                }
    }

     

    Add the following line to the Web.Config file:

    <connectionStrings>

           <add name="Portal" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|MobileDB.mdf" providerName="System.Data.SqlClient"/>

    </connectionStrings>


    Create Portal_LoginUser SPOC in your database:

    CREATE PROCEDUDE [dbo].[Portal_LoginUser]
    (
    @username NVarchar( 50 ),
    @password NVarchar( 50 )
    )
    AS
    if exists
    (
    SELECT UserName
    FROM Portal_Users
    WHERE UserName = @username
    AND UserPassword = @userpassword
    )
    RETURN 0 -- Success

    if exists
    (
    SELECT UserName
    FROM Portal_Users
    WHERE UserName = @username
    )
    RETURN 1 -- Wrong username
    RETURN 2 -- Wrong password

    Add the following web controls to the mobile web form

    <mobile:Form id="frmLogin" runat="server" Title="Login">Please enter your name and password below. <br />

            <mobile:Panel ID="pnlInvalidUsername" Runat="server" Visible="False">Username you entered is invalid!</mobile:Panel>
           
    <mobile:Panel ID="pnlInvalidPassword" Runat="server" Visible="False">The password you entered is invalid!</mobile:Panel> 
            User Name:&nbsp;<mobile:TextBox ID="txtUsername" Runat="server"></mobile:TextBox> 
            Password:&nbsp;&nbsp;<mobile:TextBox ID="txtPassword" Runat="server" Password="True"></mobile:TextBox> 
            <mobile:SelectionList ID="chkPersist" Runat="server" SelectType="CheckBox" Title="Save Session "></mobile:SelectionList>&nbsp; 
            <mobile:Command ID="cmdLogin" Runat="server" OnClick="cmdLogin_Click">Login</mobile:Command>

     </mobile:Form>

    Good luck!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2007 1:33 PM