【发布时间】: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