创建 API 的最佳方式
我们即将创建一个 API,以便我们的客户可以连接到我们、订阅服务,然后使用该服务。实现这个的最好方法是什么?我们使用 C#、.NET 进行工作。我们想要这样的东西:
我们将向客户提供用户名和密码以连接我们的 API。 (如何实现这个,使用数据库?)?
连接成功后,客户将订阅我们的服务。 (如何检查客户是否已连接?)
订阅后客户可以调用API方法。
请告诉我最好的方法。
We are about to create an API so that our customers can connect to us, subscribe to a service and then consume that service. what is the best way to implement this? we work in C#, .NET. We want something like this:
we will provide a username and password to our customer to connect with our API. (how to implement this, using database?)?
after successful connection , customer will subscribe to our service. (how to check that customer is already connected? )
after subscription customer can calls API methods.
kindly tell me the best way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你看过 servicestack 吗?它太棒了,完全基于 DTO,并且非常容易为 .net 创建 API
请查看我的 关于该内容的博客文章
检查此线程以了解 ServiceStack 中的身份验证选项。
https://groups.google.com/d/topic/servicestack/U3XH9h7T4K0/discussion
看例子此处
Have you ever look servicestack ? It's awesome ,totally DTO based and very easy to create API for .net
Please look out my blog post about that
Check this thread for authentication options in ServiceStack.
https://groups.google.com/d/topic/servicestack/U3XH9h7T4K0/discussion
Look at example here
一个简单的解决方案是提供一个Login方法,您的客户必须在使用服务之前使用用户名和密码调用此方法,一旦用户名/密码经过验证,您可以向客户端发出令牌,通常存储在数据库;然后客户端可以调用您的 API 方法,并为每个请求附加令牌,以便您可以检查客户端是否调用了 Login 方法;客户端使用完服务后,通过提供 Logout 方法或为每个令牌设置过期时间,令牌将被销毁。
这是供您考虑的。关于“最佳”方法,我认为没有一种方法适合所有情况。您会找到最适合您的方法。
A simple solution is to supply a Login method, your customers must call this one with username and password before consume the service, you can issue a token to the client once username/password validated, usually stored in a database; then the client can call your API method with the token attached for each request so you can check whether the client has called Login method or not; the token will be destroyed after the client finished using service by either providing a Logout method or setting up a expire time for each token.
This is for your consideration. About "best" way, I think there's no such one way best for all situation. You will find the best way for you.
这可能会启发您或证明有用。
http://visualstudiogallery.msdn.microsoft.com/b1d767d9-d6bf-41ee-89a8-313d338e63b6
那里还有一些链接讨论使用 API 密钥,以便您可以控制谁在使用你的 api 为什么这很重要。
This might inspire you or prove useful.
http://visualstudiogallery.msdn.microsoft.com/b1d767d9-d6bf-41ee-89a8-313d338e63b6
There are also some links in there that talk about using API keys so you can control who's using your api why that's essential.