ryan
ryan

Reputation: 53

Azure Mobile Services .net Backend Authentication using individual user accounts

I have an existing ASP.net Web API that uses Individual User Accounts.

I use it to Signup / ChangePassword / Login etc. When I login, I receive back a token and then use the token to access the [Authorize] Controllers.

i.e.
[Authorize] public class ValuesController : ApiController

I have added the Controllers, DataObjects , Startup.MobileApp and AppNameApiContext from the TODO .net backend for Azure mobile apps to my current API.

i.e public class TodoController : TableController

This works fine for syncing / inserting etc. but if I add [Authorize] to the controller it will always return unauthorized even if I send the same token that I'm using with the other API endpoints.

All the samples I can find using "Custom Authentication for Azure Mobile Apps" use Microsoft.Azure.Mobile.Server.Authentication.

How do I use the same login/token to provide authentication to both (ValuesController : ApiController ) and the (TodoController : TableController)

Many thanks in anticipation

I'm trying to find a solution like

app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
            {
                SigningKey = signingKey,
                ValidAudiences = new[] { hostName },
                ValidIssuers = new[] { hostName },
                TokenHandler = new MyClass (config)
            });


public class MyClass : AppServiceTokenHandler
{
  public override bool TryValidateLoginToken
  (string token,
   string signingKey,
   IEnumerable<string> validAudiences,
   IEnumerable<string> validIssuers,
   out ClaimsPrincipal claimsPrincipal)

  {
     if (CheckIfUserIsAuth(token))
     {
       return true;
     }

     // Or something like 
     // if(token== CurrentUserToker)
     //  { return true};
  }
}

However I still receive unauthorized, whether I return true of false.

Upvotes: 0

Views: 419

Answers (1)

Adrian Hall
Adrian Hall

Reputation: 8035

You have started from the wrong point, unfortunately. You need to start from an Azure Mobile App. This has the necessary logic in it to authenticate from a mobile device using EasyAuth. I recommend starting from the tutorial - it has both a frontend and a backend of your choice. You can then expand as necessary.

Upvotes: 0

Related Questions