在 WCF Webapi 站点上验证 Android 客户端?

发布于 2024-12-22 14:47:13 字数 137 浏览 1 评论 0原文

我想用 C# 实现 REST-api。我发现 WCF Webapi 可以做到这一点。 我的第一个问题是如何只允许经过身份验证的用户访问我的 api? 第二个问题是,如果待认证的客户端是Android设备,如何进行HTTP请求进行认证?

谢谢!

I want to implement a REST-api in C#. I found that WCF Webapi can do that.
My first question is how I can give only authenticated users access to my api?
And the second question is if the client to be authenticated is a Android-device, how do I do the HTTP request to authenticate?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-12-29 14:47:13

嗯……
我们做了类似的事情,我们使用基本身份验证+HTTPS,
这意味着用户名和密码将在每个请求的 http 标头中传递。

因此,在您的网络服务中,您可以进行身份​​验证,如果它来自无效用户,则将其踢出。

或者,您可以为每个客户端生成一个 GUID,然后要求将 GUID 与每个 http 请求一起传递回搜索,验证 GUID。

在 Android 设备上,当您发出 http 请求时,添加 http 标头

Authorization:Basic ****

非常简单,这里是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);

em...
We did similar things, we use basic authentication+HTTPS,
that means the user name and password will be passed along each request, in the http header.

Thus in your web service, you can authenticate then, if it is from not valid user, then kick them out.

Or alternatively you can generate a GUID for each of your client, ask then to pass the GUID back to the search along with each http request, authenticate the GUID.

on Android device , when you send out the http request , add an http header

Authorization:Basic ****

quite easy , here is a codesnipet on android

    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文