我当前的 WCF REST 方法定义为:
[OperationContract]
[WebGet(UriTemplate = "{username}/{password}", ResponseFormat =
WebMessageFormat.Json)]
string Login(string username, string password);
Android 客户端应用程序将连接到该服务,假设它是 http://service.com/login.svc/login ...
但我不想像我在 UriTemplate 中指定的那样在 url 中传递用户名和密码。我怎样才能从 android 应用程序接收用户名和密码到我的服务中,或者更好的是,我怎样才能更改我的登录方法以在我可以在我的登录函数中处理的一些 POST 参数中检索用户名和密码并验证用户一个 sql 成员(member)数据库。
最佳答案
我们已通过使用“授权” header 完成此操作。客户传递一组加密的凭据,我们在我们这边为他们生成一个 token 。下面是处理身份验证的 HttpModule 的 BeginRequest 方法示例。我们使用自定义主体来处理 token :
private void BeginRequest(Object source, EventArgs e)
{
if (null == HttpContext.Current || String.IsNullOrEmpty(HttpContext.Current.Request.Headers["Authorization"]))
{
HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.Unauthorized;
HttpContext.Current.Response.End();
}
HttpContext context = HttpContext.Current;
Regex matcher = new Regex(WfmConfigurationManager.GetAppSetting("AuthenticationPath"));
if (!matcher.IsMatch(context.Request.Url.ToString(),0))
{
String authHeader = context.Request.Headers["Authorization"];
IIdentity tokenIdentity = new TokenIdentity(authHeader);
if (!tokenIdentity.IsAuthenticated)
{
HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.Unauthorized;
HttpContext.Current.Response.End();
}
IPrincipal tokenPrincipal = new TokenPrincipal(tokenIdentity, TokenAuthentication.GetRolesForUser(tokenIdentity));
HttpContext.Current.User = tokenPrincipal;
}
}
关于c# - 将从 Android 应用程序发送的用户名和密码读入我的 WCF REST 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3841758/