【问题标题】:asp.net identity webservice androidasp.net 身份 web 服务 android
【发布时间】:2017-09-18 20:51:38
【问题描述】:

我有一个简单的 ASP.net webform 应用程序,带有用于身份验证的 identity+owin。这工作得很好。

现在我需要创建一个 webservice(.asmx) 来让我的用户通过服务进行身份验证。因此服务可以接受用户名+密码并对其进行身份验证。

这是对 android 应用程序的用户进行身份验证所必需的。

我不想使用任何其他身份验证,例如 google 或 facebook..etc...

也没有 MVC。

谁能指点我的解决方案。

下面是我的 login.aspx 页面的代码。我应该在我的网络服务中添加什么?

Android 应用程序开发人员是另一个人。我只需要确保我的网络服务工作正常。

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");
        }

【问题讨论】:

    标签: c# android asp.net web-services asp.net-identity


    【解决方案1】:

    您需要在网络服务中使用 Post&Get 方法。

    创建新的 android 应用并为 Http Req 使用 volley 或本机 java 库。

    这是请求的示例代码;

    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();
        }    
    }
    

    您的网络服务将响应该请求。

    例如:yoursite.com?name=foo?pass=foo RESPONSE->真或假

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 2013-06-11
      • 2020-11-17
      • 2016-07-17
      • 1970-01-01
      • 2017-07-26
      • 2019-03-24
      • 2020-05-06
      • 2023-03-11
      相关资源
      最近更新 更多