我想用 C# 实现 REST-api。我发现 WCF Webapi 可以做到这一点。 我的第一个问题是如何只允许经过身份验证的用户访问我的 api? 第二个问题是,如果待认证的客户端是Android设备,如何进行HTTP请求进行认证?
谢谢!
最佳答案
他们... 我们做了类似的事情,我们使用基本身份验证+HTTPS, 这意味着用户名和密码将在每个请求的 http header 中传递。
因此,在您的网络服务中,您可以进行身份验证,如果它来自无效用户,则将其踢出。
或者,您可以为每个客户端生成一个 GUID,然后要求将 GUID 与每个 http 请求一起传递回搜索,验证 GUID。
在Android设备上,当您发出http请求时,添加http header
授权:基本****
很简单,这是 Android 上的codesnipet
String baseUrl = this.getValue(ServiceBaseUrlKey);</i>
DefaultHttpClient client = new ConnectionManager().getHttpClient();//create a httpclient
HttpGet request = new HttpGet();
request.setURI(new URI(baseUrl + "Path"));
//TODO need to wrap up how to apply the basic authentication.
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("UserName", "****");
request.addHeader(new BasicScheme().authenticate(credentials, request));
request.addHeader("Content-Type","Application/JSON");
HttpResponse response = client.execute(request);
关于c# - 在 WCF Webapi 站点上验证 Android 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8585588/