Mashhoor Gulati
Mashhoor Gulati

Reputation: 127

asp.net identity webservice android

I've a simple ASP.net webform application with identity+owin for authentication. This works perfectly fine.

Now I need to create a webservice(.asmx) to get my users authenticated through service. So the service can accept the UserName+Password and authenticate them.

This is required to authenticate the users for android app.

I don't want to use any other authentication like google or facebook..etc...

And no MVC either.

Can anyone pls point me towards solution.

bellow is my login.aspx page's code behind. What should I put in my webservice.?

Android app developer is another guy. I just need to make sure my webservice works fine.

protected void SignIn(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            var user = userManager.Find(UserName.Text, Password.Text);

            if (user != null)
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);


                string cs = ConfigurationManager.ConnectionStrings["DB_6"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(cs))
                {
                    SqlCommand cmd = new SqlCommand("spGetUser", conn);
                    SqlDataAdapter Adpt = new SqlDataAdapter();
                    DataSet login = new DataSet();
                    try
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@PEmail", UserName.Text);
                        Adpt.SelectCommand = cmd;
                        Adpt.Fill(login);
                        foreach (DataRow dr in login.Tables[0].Rows)
                        {
                            string PFName = login.Tables[0].Rows[0]["PFName"].ToString();
                            string PLName = login.Tables[0].Rows[0]["PLName"].ToString();
                            string PType = login.Tables[0].Rows[0]["PType"].ToString();
                            int PersonID = int.Parse(login.Tables[0].Rows[0]["PersonID"].ToString());
                            using (SqlConnection con = new SqlConnection(cs))
                            {

                                SqlCommand cmd1 = new SqlCommand("spInsLoc", con);
                                cmd1.CommandType = CommandType.StoredProcedure;
                                cmd1.Parameters.Add("@PersonID", SqlDbType.Int).Value = PersonID;
                                cmd1.Parameters.Add("@OrgID", SqlDbType.Int).Value = 0;
                                cmd1.Parameters.Add("@Location", SqlDbType.NVarChar).Value = hdnLocation.Value;
                                cmd1.Parameters.Add("@strOwner", SqlDbType.VarChar).Value = UserName.Text;
                                cmd1.Parameters.Add("@dbTstamp", SqlDbType.DateTime2).Value = DateTime.Now;
                                con.Open();
                                cmd1.ExecuteNonQuery();

                            }
                        }
                    }
                    catch { }
                    finally { }

                }

                Response.Redirect("~/Login.aspx");
            }
            else
            {
                StatusText.Text = "Invalid username or password.";
                LoginStatus.Visible = true;
            }
        }

protected void SignOut(object sender, EventArgs e)
        {
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            authenticationManager.SignOut();
            Response.Redirect("~/Login.aspx");
        }

Upvotes: 0

Views: 154

Answers (1)

Berat Ey&#252;boğlu
Berat Ey&#252;boğlu

Reputation: 1733

You need use Post&Get method in web service.

Create new android app and use volley or native java library for Http Req.

this is sample code for a request;

URL url;
HttpURLConnection urlConnection = null;
try {
    url = new URL("http://yoursite.com?name=foo?pass=foo");

    urlConnection = (HttpURLConnection) url
            .openConnection();

    InputStream in = urlConnection.getInputStream();

    InputStreamReader isw = new InputStreamReader(in);

    int data = isw.read();
    while (data != -1) {
        char current = (char) data;
        data = isw.read();
        System.out.print(current);
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }    
}

and your web service will response the request.

ex: yoursite.com?name=foo?pass=foo RESPONSE->TRUE or FALSE

Upvotes: 1

Related Questions