We want to create an azure function in c# that retrieve the list of azure web app contained in the subscription (basically we want to call dynamically, for each webapp, the same API endpoint changing the subdomain of the api).
It's possible with c# retrieve the list of the web app contained in the same azure function subscriptions?
Usually we connect to the master database, we query the sys.databases to collect the dbname and understand the webapp names. But we are searching for a smartest way.
If you're in C# land, I'd look at using the ArmClient class to retrieve what you're looking for.
Install these (I've got a few others installed but start with that and see how you go, there may be a couple of others needed) Nuget packages ...
Azure.Identity;
Azure.ResourceManager;
Azure.ResourceManager.AppService
... and from there, using the DefaultCredential approach (if you've never used it, read up on it here -> https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/identity/Azure.Identity/README.md) you can query your subscriptions webApps ...
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.AppService;
using System;
using System.Threading.Tasks;
namespace AzureManagement
{
internal class Program
{
static void Main(string[] args)
{
GetAzureResources().Wait();
}
static async Task GetAzureResources()
{
var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential);
var subscription = await armClient.GetDefaultSubscriptionAsync();
var webSitesEnumerator = subscription.GetWebSitesAsync().GetAsyncEnumerator();
try
{
while (await webSitesEnumerator.MoveNextAsync())
{
var webSite = webSitesEnumerator.Current;
Console.WriteLine($"Web App Name ........ {webSite.Data.Name}");
Console.WriteLine($"Default Host Name ... {webSite.Data.DefaultHostName}\n");
}
}
finally
{
await webSitesEnumerator.DisposeAsync();
}
Console.ReadLine();
}
}
}
The above is obviously not a function app but the core code will still work for you and can be ported as need be.
Note: I could be telling you how to suck eggs, but, once deployed to Azure, you'll need to do the necessary work to ensure that the function app has the required access to retrieve all of the resource information you're looking for.
If you're unfamiliar with that, read up on the managed identity concept. It's very easy to setup -> https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity
Yes, one easy way is to use HttpClient and send a request to Azure Rest API:
GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Web/sites?api-version=2022-03-01
https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/list
PS: you first need to acquire an authentication token.
https://www.youtube.com/watch?v=6b1J03fDnOg&t=329s
Related
Background
I've created a working bot in C# but I'm failing to expand it to be a multi-tenant bot. I have created multiple bots in the Microsoft portal using this technique to identify themselves from the messaging endpoint:
https://example.com/api/messages/bot1
https://example.com/api/messages/bot2
https://example.com/api/messages/bot3
I can grab the LastSegment from the URL while in the MessagesController and store it in PrivateConversationData so I know which bot is talking in the current conversation. I intended use this stored 'bot id' in order to retrieve the Microsoft AppId & Password from the web.config (the bot's credentials are stored as a series of custom entries and not the standard appSettings as that only works for a single bot).
Credentials Problem
The authentication works well (nearly) as described here except when using async code with .ConfigureAwait(false) I can't get the HttpContext.Current as it becomes null when running on a different thread. This means I can't get the authenticated user's credentials either by looking them up in the web.config or by calling GetCredentialsFromClaims() since I've lost the authenticated user. If I use .ConfigureAwait(true) I just get deadlocks all over the place.
I have the credentials in the web.config but they are stored per bot and I need the 'bot id' from the URL above in order to get the credentials.
Question
The crux of the problem is: I need the URL to get the 'bot id' and I need the 'bot id' to get the credentials from the web.config but I can never reliably get access to the URL once I've passed a .ConfigureAwait(false) in the code. On the flip side, I can't get the 'bot id' from the PrivateConversationData since I need the bot's credentials in order to load it. A bit chicken and egg :-(
If anyone has any ideas of what I may be doing wrong or has an alternative approach to know which 'bot id' is currently executing I'd very much appreciate it.
Thanks
Please find below given the sample code.
public class StartUp {
public void Configuration(IAppBuilder app) {
var builder = new ContainerBuilder();
//Note: Initialize / register the Metadata Service that can bring the tenant details from the corresponding store
builder.RegisterType<TenantMetadataService>().As<ITenantMetadataService>();
//Note: This helps you in accessing the TenantMetadata from any constructor going forward after the below registry
builder.Register(ti => TenantMetadata.GetTenantMetadataFromRequest()).InstancePerRequest();
//TODO: Register the various services / controllers etc which may require the tenant details here
}
}
public class TenantMetadata {
public Guid TenantId { get;set; }
public Uri TenantUrl { get;set; }
public string TenantName { get;set; }
public static TenantMetadata GetTenantMetadataFromRequest() {
var context = HttpContext.Current;
//TODO: If you have any header like TenantId coming from the request, you can read and use it
var tenantIdFromRequestHeader = "";
//TODO: There will be a lazy cache that keeps building the data as new tenant's login or use the application
if(TenantCache.Contains(...))return TenantCache[Key];
//TODO: Do a look-up from the above step and then construct the metadata
var tenantMetadata = metadataSvc.GetTenantMetadata(...);
//TODO: If the data match does not happen from the Step2, build the cache and then return the value.
TenantCache.Add(key,tenantMetadata);
return tenantMetadata;
}
}
Note
The above code snippet uses the various service placeholders, cache and the other methods which will require to be used based on the designed application services. If you wish not to cache the tenant metadata, if it may contain some sensitive data, you can remove the caching implementation parts.
This implementation can be spread across all your web facing portals like your Web UI, Web Api and WebJobs etc so that it is same across all apps and it is easy to test and consume.
HTH.
I'm trying to use Auth0 for my mobile application (Xamarin) and I've been following the quickstart guide provided by Auth0.
I've installed the component via Xamarin market (Visual Studio) and to do the integration, I've created a class file dedicated to Auth0 related operations. I copied and pasted the provided code from the quick start guide but visual studio returns this error:
The contextual keyword 'var' may only appear within a local variable declaration or in script code
The code provided used var instead of a normal variable so I'm not sure what I can substitute with it:
using Auth0.SDK;
namespace Application
{
class LoginHandler
{
var auth0 = new Auth0Client(
"*************",
"************************");
}
}
I'm not sure what I'm doing wrong, I'd also like some help implementing the Login UI as the Xamarin guides are deathly outdated.
Short answer: You can use Auth0Client instead of var.
internal class LoginHandler
{
private Auth0Client auth0 = new Auth0Client("***", "*****");
}
Long answer:
The snippet is assuming you would use the Auth0 client instance in a context where it would be considered a local variable and as such it would be valid code. For example:
internal class LoginHandler
{
public void HandleLogin()
{
var auth0 = new Auth0Client("***", "*****");
// ...
}
}
If you require assistance in solving other specific issues then you should post specific questions that clearly illustrate the problem you're having.
So I am using Nancy with Nowin.
The beauty of using Nowin is I don't have to mess around with various Windows commands to set up a simple web server. According to the Nowin readme I can configure SSL using the following line
builder.SetCertificate(new X509Certificate2("certificate.pfx", "password"));
However, when using Nancy I don't seem to have access to this Server builder class. Everything seems to happen magically behind the scenes.
Any ideas how I can pass the certificate through to Nowin?
Make sure you have the Nancy.Owin package installed.
Use code like this to start the server up:
.
using System;
using System.Net;
using System.Threading.Tasks;
using Nancy.Owin;
using Nowin;
public class Program
{
static void Main(string[] args)
{
var myNancyAppFunc = NancyMiddleware.UseNancy()(NancyOptions options =>
{
// Modify Nancy options if desired;
return Task.FromResult(0);
});
using (var server = ServerBuilder.New()
.SetOwinApp(myNancyAppFunc)
.SetEndPoint(new IPEndPoint(IPAddress.Any, 8080))
.SetCertificate(new X509Certificate2("certificate.pfx", "password"))
.Build()
)
{
server.Start();
Console.WriteLine("Running on 8080");
Console.ReadLine();
}
}
}
If you look at this document, it says the following:
Configuration of OWIN
It'll just be there if the host sends it on.
If you use IIS as a host. You'll need to do the same config as with Aspnet. And you'll need an OWIN Aspnet host that supports the ClientCertificate. The one in the OWIN demo in Nancy does. The one by #prabirshrestha also does.
In the OWIN Demo, check this line:
if (request.ClientCertificate != null && request.ClientCertificate.Certificate.Length != 0)
{
env[OwinConstants.ClientCertificate] = new X509Certificate(request.ClientCertificate.Certificate);
}
Hope it helps you, good luck.
I think you should follow by the way described in this article: https://msdn.microsoft.com/en-us/magazine/dn451439.aspx
At first you are creating web server according with Nowin documentation and after that you are adding Nancy as pipeline component. I tested this way with NowingSample (from Nowin package) and it works.
I am using Azure Mobile Services to provide data to a Windows Universal app and Azure API Management as a proxy for API usage and analytics purposes. This is working great.
Now I was asked to provide offline functionality to the application so I started using Azure Mobile Services Synchronization Context in order to implement this using SQLite as the local store.
Azure API Management requires me to send my subscription key as part of my query string. I have been doing this using the 'parameters' dictionary provided by the IMobileServiceTable.InsertAsync method and this was working fine as well.
Now the offline implementation requires me to use IMobileServiceSyncTable.InsertAsync method instead, which doesn't provide an overload with the 'parameters' dictionary. The MobileServiceSyncContextExtensions.PushAsync method does not seem to provide a way to add custom parameters to the query string either.
Does anyone know of a way to include custom parameters when using the Mobile Services Synchronization Context in order to send the subscription key of the Azure API Management service?
I've found the way to do this.
I implemented the following HTTP Message Handler:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
class AzureApiManagementHandler : DelegatingHandler
{
string _subscriptionKey;
public AzureApiManagementHandler(string subscriptionKey)
{
_subscriptionKey = subscriptionKey;
}
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
var baseUri = new UriBuilder(request.RequestUri);
string queryToAppend = string.Format("subscription-key={0}", _subscriptionKey);
if (baseUri.Query != null && baseUri.Query.Length > 1)
baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend;
else
baseUri.Query = queryToAppend;
request.RequestUri = baseUri.Uri;
return base.SendAsync(request, cancellationToken);
}
}
And then I passed it to the Mobile Services client in the constructor:
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://yoursubdomainhere.azure-api.net",
"yourapikeyhere",
new AzureApiManagementHandler("yoursubscriptionkeyhere")
);
I hope this is useful for anyone facing the same problem.
I'm trying to find what I'm doing wrong regarding an Azure WebJobs QueueTrigger method that should be triggered from an Azure Storage Queue.
I've read a couple of documents (as in blog posts / msdn articles). But I'm still not clear.
Main question / misunderstood aspect:
What should be the name of the connection string for Azure storage console app App.config or Windows Azure Configuration (portal). So far I have the following name set at both places.
AzureJobsStorage
AzureWebJobsStorage
AzureJobsRuntime
AzureJobsDashboard
AzureJobsData
Here's my WebJobs console app code.
static void Main()
{
JobHost host = new JobHost();
host.RunAndBlock();
}
public static void CreateLeague([QueueTrigger("temp")] string msg)
{
var task = JsonConvert.DeserializeObject<QueueTask>(msg);
if (task.TaskType == QueueTask.TaskTypes.Pdf)
RenderPdf(task.Id);
}
This console app is continuously running on my Azure Website.
I can access its "debug" page where I can toggle output and I see it is started / running.
My code to add queue (from my ASP.NET MVC app):
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("temp");
queue.CreateIfNotExists();
Common.QueueTask task = new Common.QueueTask();
task.TaskType = Common.QueueTask.TaskTypes.Pdf;
task.Id = p.Id;
CloudQueueMessage msg = new CloudQueueMessage(JsonConvert.SerializeObject(task) );
queue.AddMessage(msg);
This code is executed, and queue are added to my Storage Account. But they did not get "dequeue" or read from the WebJobs.
Hmm, the WebJobs class had to be public.
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using Proceed.Common;
using System;
using System.Configuration;
using System.IO;
public class WebJobsTask {
public static void Main()
{
JobHost host = new JobHost();
host.RunAndBlock();
}
public static void CreateLeague([QueueTrigger("temp")] string msg)
{
var task = JsonConvert.DeserializeObject<QueueTask>(msg);
if (task.TaskType == QueueTask.TaskTypes.Pdf)
RenderPdf(task.Id);
}
}
Also found a quick way to explore my queues: https://azurestorageexplorer.codeplex.com/.
In my case, I had assumed that QueueTrigger was referring to Service Bus Queues instead of Azure Queues, and I actually needed to use ServiceBusTrigger.
You can use the server explorer in VS to explore the content of the Storage queues.
The queue triggers for the WebJobs SDK will exponentially back off if there is no work to do. There might be a delay between the moment a message is put in a queue and the moment when it is picked up. You can configure the maximum back off through the JobHostConfiguration.Queues.MaxPollingInterval property.
For the latest SDK you need two storage connection strings AzureWebJobsStorage and AzureWebJobsDashboard
This is a great place for more resources: https://learn.microsoft.com/en-us/azure/app-service-web/websites-webjobs-resources