2017-04-21 79 views
0

我有一個簡單的ASP.net webform應用程序身份+ owin進行身份驗證。這工作非常好。asp.net identity webservice android

現在我需要創建一個web服務(.asmx)來讓我的用戶通過服務進行身份驗證。所以服務可以接受用戶名和密碼並驗證它們。

這是驗證Android應用程序的用戶所必需的。

我不希望使用任何其他身份驗證像谷歌或facebook..etc ...

而且沒有任何MVC。

任何人都可以指出我的解決方案。

波紋管是我的login.aspx頁面的代碼。我應該在我的web服務中放入什麼?

Android應用程序開發人員是另一個人。我只需要確保我的web服務工作正常。

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

回答

1

您需要使用文章&獲取Web服務的方法。

創建新的android應用程序並使用抽象或原生Java庫來獲取Http Req。

這是請求的示例代碼;

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

並且您的Web服務將響應請求。

例如:yoursite.com?name=foo?pass=foo響應 - > TRUE或FALSE

+0

我已經更新的問題。請見。 –