I am writing a Web API using MVC4 that should be consumed by multiple client types. I want to use the OpenID to authenticate.
I already have downloaded the DotNetOpenAuth NuGet package, but so far all of the examples are for a client app, rather than an API.
My problem is simple. I want to have clients send an authentication request to my API. The API authenticates with an OpenID provider. The API then sets whatever it needs to in order to use the [Authorize] tags throughout the web api calls.
I understand that in .NET applications, that the FormsAuthentication.SetCookie could be called, but is this also an easy-to-implement solution for other languages?
The question in a nutshell. How do I integrate OpenID into an MVC4 web api that allows for the use of the Authorize tag that can be called and consumed by multiple languages?
OpenID Connect (OIDC) is an identity layer built on top of the OAuth 2.0 framework. It allows third-party applications to verify the identity of the end-user and to obtain basic user profile information. OIDC uses JSON web tokens (JWTs), which you can obtain using flows conforming to the OAuth 2.0 specifications.
OpenID vs. OAuth. Simply put, OpenID is used for authentication while OAuth is used for authorization. OpenID was created for federated authentication, meaning that it lets a third-party application authenticate users for you using accounts that you already have.
OpenID Connect 1.0 is a simple identity layer on top of the OAuth 2.0 protocol. It allows Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server, as well as to obtain basic profile information about the End-User in an interoperable and REST-like manner.
You may be confusing the roles of authentication and authorization. It sounds like your Web API needs both.
Let's start with authorization. Every API (that is, a web URL that is accessed by a client app other than a browser) either permits anonymous access or must be authorized (i.e. authorization). Authorization is OAuth's domain. OAuth (v2, presumably) describes how a client authorizes a call to your WebAPI.
Presumably as part of the authorization process, a user logs into your service. This step of logging in the user is authentication. And it is orthogonal to authorization. Whether you authenticate the user via OpenID, username/password, X.509 cert, etc., should be irrelevant to how your WebAPI calls are authorized. In other words, your WebAPI methods shouldn't care how the user authenticated (read: no OpenID ties whatever). What they'll have is an authorization filter applied to them that verifies the authorization on an incoming request and translates it to a few pieces of information including the username of the account that authorized the access, the level of access, the id of the authorized client, etc.
So a step at a time, the whole scenario might go something like this:
Controller.User
is null (or User.Identity.IsAuthenticated
is false
). Refer to the OAuthAuthorizationServer sample for how to implement this endpoint.redirectUrl
parameter in the query string that retains the full incoming OAuth 2 authorization request URL.redirectUrl
argument. Refer to the OpenIdRelyingPartyMvc sample for how to do this.AuthorizationServer
to create the authorization record and return the response to the client. One of the results of this call is the formulation of a redirect response to the client that gives it an authorization code.WebServerClient
class to exchange the authorization code for an access token (and usually a refresh token as well). That's the whole story. And yes, the client role is easy to write regardless of language or library that they happen to be using.
BTW, the DotNetOpenAuth samples I refer to are not distributed via NuGet. You get the samples from SourceForge.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With