c# - 将从 Android 应用程序发送的用户名和密码读入我的 WCF REST 服务?

标签 c# android wcf rest

我当前的 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/

相关文章:

c# - 为什么我们不能在 C# 中进行 IntPtr 和 UIntPtr 运算?

c# - 使用 FileStream 和这些选项 c# 读取文本文件的实际内容

android - 如何在没有实时更新的情况下获取经纬度?

c# - 通过 REST 发送大量数据 - 最佳实践

wcf - 如何在没有iis的情况下自行托管wcf

c# - Wcf 服务返回流但不会关闭

c# - 如果您限制输入的最大长度,是否存在验证文本框输入的安全原因?

c# - Entity Framework 6 CF : Remove One-to-Many

android - 在 Android 中将闹钟设置为特定日期和时间

Android - 转换为 Dalvik 格式失败 : Unable to execute dex: Java heap space