【问题标题】:MVC4 - Android authentication via http POST and FormsAuthenticationMVC4 - 通过 http POST 和 FormsAuthentication 进行 Android 身份验证
【发布时间】:2014-10-12 16:05:30
【问题描述】:

我知道网上有很多这样的资源,我最接近的是这个问题的答案:ASP.NET Web API Authentication

基本上,这是我的要求。在我创建的 MVC4 互联网应用程序(使用 SimpleMembership)上通过 android 登录我的帐户。它不是一个 MVC Web Api 应用程序,在查看实现此目标的各种方法时,它似乎使事情变得混乱。

我正在尝试使用 FormsAuthentication 设置身份验证 cookie,但我不知道如何配置我的 android httpclient 以实际通过此身份验证 cookie 发送,或者如何让 MVC 从我的 android 应用程序中保存会话。

到目前为止,这是我在 MVC 方面提出的:

    [HttpPost]
    [AllowAnonymous]
    public bool LoginMobi(LoginModel model)
    {    
        var membership = (SimpleMembershipProvider)Membership.Provider;
        if (membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, false);            
            return true;
        }
        else return false;
    }

我在我的 android 应用程序中使用以下 java(通过 SSL 连接发送):

            DefaultHttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("https://mysite/api/login");
            List<NameValuePair> nameValue = new ArrayList<NameValuePair>();
            nameValue.add(new BasicNameValuePair("UserName", "foo"));
            nameValue.add(new BasicNameValuePair("Password", "bar"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValue));
            httppost.setHeader("Content-type", "application/json");
            HttpResponse response = httpclient.execute(httppost);
            // etc etc

我还没有弄清楚如何在 android 上接收身份验证 cookie 并将其与每个请求一起发送回具有 [Authorize] 属性的控制器。我对此比较陌生,所以请原谅我的无知!

【问题讨论】:

    标签: android asp.net asp.net-mvc cookies forms-authentication


    【解决方案1】:

    您正在使用 FormsAuthentication,它使用 cookie 来识别每个请求的用户。您有两种选择。

    1. 为 HttpClient 使用 CookieStore。检查Android HttpClient and Cookies

    2. 结合 BASIC auth 和 FormsAuthentication。检查Combining Forms Authentication and Basic Authentication

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2014-05-26
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多