views:

64

answers:

2

Is this possible?

I'd like to expose a URL (action) such as http://mysever/myapp/UpdateHeartbeat/.

In my MVC application it looks like

[Authorize]
[AcceptsVerbs(HttpVerbs.Post)]
public ActionResult UpdateHeartbeat()
{
    // update date in DB to DateTime.Now
}

Now, in my MVC application the user has logged in via FORMS authentication and they can execute that action to their hearts content.

What I want to do, is hit that URL from a Console application and be able to authenticate (as part of an API that I wouldl like to build) -- is there a way I can do that without removing the [Authorize] attribute and adding username/password as parameters to the POST?

A: 

The Authorize filter actually gets the IPrincipal for the user from the current context so this would not be possible. You will want an alternate form of auth for that method.

A quick google search provides a link to the following blog post that may be of use:

http://davidhayden.com/blog/dave/archive/2009/04/09/CustomAuthorizationASPNETMVCFrameworkAuthorizeAttribute.aspx

Bradley Mountford
So, would I then use Basic HTTP Authentication (over https/ssl obviously) and check those credentials in the overridden `AuthorizeCore();` method??
Nate Bross
Sure. You are writing the logic in the override so you can do whatever you want to make it return true or false.
Bradley Mountford
I know, I guess my question is, are there any implications to doing that, which I might not be aware? Is it secure? assuming the URI is HTTPs and ssl encrypted, are the credentials then also encrypted?
Nate Bross
As long as you are sending the credentials within the SSL payload, then you are secure (at least as secure as SSL can be considering its known vulnerabilities i.e. MITM etc.). Don't put your credentials in a query string, obviously (I've actually seen that done).
Bradley Mountford
+1  A: 
Luke
This isn't for testing, this is for building an API which will be accessed by non .NET clients, but it still needs to be secure.
Nate Bross
I understand that. The same principles apply, however, in that you are asking to call a controller action that requires forms authentication. This code mocks the forms authentication. You still have the ability to authenticate the users via your API in any way you choose, prior to passing in the "authenticated" user.
Luke
Sorry, I missed the portion you had mentioned about adding the username/password as part of the post. If that is the case, I would personally create a separate action that takes username/password as parameters, does the authentication, then redirects to the UpdateHeartbeat() action.
Luke