English 中文(简体)
What s the simplest way to do authentication with a web API?
原标题:

I ve got a web API that provides data to users without authentication (the website lets users post data, after they ve logged in using traditional cookies & sessions). Someone wants to develop an iPhone app that adds things to my database, so I want a user to authenticate on the iPhone, and then the api will allow posting.

So, what should I look in to do this easily? The API as it stands is RESTful, it d be nice to keep it that way. Obviously I m new to this but there seem to be so many standards I don t know where to start. If I can code it up in less than an hour, that d be ideal.

Much appreciated!

最佳回答

A decent way to implement this would be to provide the app creator with a token as well as an app id, and have them use that token as salt for an agreed upon encryption method to send username and password (plus app id) to a new API call for a new session.

Upon receiving the request for a new session, you would look up their token based on the appid provided, and try and decrypt the user/pass using the token.

If the user/pass are accepted, then you create a new session and return the session id to them, which they can send along with any new requests.

This prevents the app from having to send authentication for every request, and allows you to expire sessions at a given time.

问题回答

WebSecurity was introduced in ASP.NET MVC 4. It relies on the SimpleMembershipProvider. It uses FormsAuthentication to manage cookies

WebMatrix.WebData.WebSecurity is provides security and authentication features for ASP.NET Web Pages applications, including the ability to create user accounts, log users in and out, reset or change passwords, and perform related tasks.

You must create or initialize an WebSecurity database before you can use the WebSecurity object in your code.

In the root of your web, create a page (or edit the page ) named _AppStart.cshtml.

  _AppStart.cshtml

    @{
    WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId", "Email", true);
    }

you can authenticate your request by following code.

WebSecurity.Login(LoginName, Password, true)

once authenticated successed , you will get value of WebSecurity.IsAuthenticated is true and you will get user s identity

you can also use "SimpleRoleProvider" for manage roles in your application





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签