No application keys for Azure Mobile Apps - what's a simple replacement?

我们两清 提交于 2019-12-08 03:28:14

问题


I've been using Azure Mobile Services and now I created one of the new Mobile Apps via the all new Azure Portal.

While using Mobile Services it was possible to limit API access via an application key. The concept of this key no longer applies to Mobile Apps it seems.

All I need is a really lightweight protection of my services, exactly what the Application Key did. I just want to prevent that everybody out there navigates to my Azure app and messes around with my database; the App Key was perfect for those cases when you did not have anything to hide but wanted to prevent "spamming".

I see there is now Active Directory integration as an alternative but unfortunately I cannot find a guide how to move from App Key to something else.


回答1:


Check this post How to configure your App Service application to use Azure Active Directory login

this authentication sample code works with UWP

private async Task AuthenticateAsync()
        {
            while (user == null)
            {
                string message=string.Empty;

            var provider = "AAD";

            PasswordVault vault=new PasswordVault();
            PasswordCredential credential = null;

            try
            {
                credential = vault.FindAllByResource(provider).FirstOrDefault();
            }
            catch (Exception)
            {
                //Ignore exception
            }
            if (credential != null)
            {
                // Create user
                user = new MobileServiceUser(credential.UserName);
                credential.RetrievePassword();
                user.MobileServiceAuthenticationToken = credential.Password;

                // Add user
                App.MobileServiceClient.CurrentUser = user;

                try
                {
                    //intentamos obtener un elemento para determinar si nuestro cache ha experidado
                    await App.MobileServiceClient.GetTable<Person>().Take(1).ToListAsync();
                }
                catch (MobileServiceInvalidOperationException ex)
                {
                    if (ex.Response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        //remove expired token
                        vault.Remove(credential);
                        credential = null;
                        continue;
                    }
                }
            }
            else
            {
                try
                {
                    //Login
                    user = await App.MobileServiceClient
                        .LoginAsync(provider);

                    //Create and store credentials
                    credential = new PasswordCredential(provider,
                        user.UserId, user.MobileServiceAuthenticationToken);
                    vault.Add(credential);
                }
                catch (MobileServiceInvalidOperationException ex)
                {
                    message = "You must log in. Login Required";
                }
            }
            message = string.Format("You are now logged in - {0}", user.UserId);
            var dialog = new MessageDialog(message);
            dialog.Commands.Add(new UICommand("OK"));
            await dialog.ShowAsync();

        }
    }


来源:https://stackoverflow.com/questions/33506820/no-application-keys-for-azure-mobile-apps-whats-a-simple-replacement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!