Logging in to Azure AD from Windows Forms desktop app

I have an Azure Function that slices and processes a .CSV file uploaded to Azure Blob Storage, and stores the data extracted from it into a MS Dynamcis (DataVerse) system. That function works just fine.

Now, I would like to build a very simple GUI tool on Windows, to allow some computer novices to pick the CSV file they want to have processed, and upload it to Azure Blob Storage. It’s a small Winforms desktop app for now.

My challenge is: how to authenticate against Azure AD to be able to upload the file into Azure Blob storage?

I did try to use a client id (Azure app registration), a tenant id, and a client secret – and that works quite nicely.

BUT: how do I securely handle these bits of information, on a desktop computer?

  • I could put the client id, client secret and tenant id into environment variables – but that would not be very safe, really
  • I could put the client id, client secret and tenant id into an on-disk config file, like appsettings.json – but that again would not be very safe

As a solution, I thought of putting these bits into Azure Keyvault, and get them from there, to use in my desktop app – yeah, sure – but how can I authorize against Azure Keyvault, then, to get these bits?

I have found a lot of code samples that tell me to use DefaultAzureCredentials to log in to Azure – which seems like a really good idea – with the caveat that it doesn’t work in my environment (and I have no idea, why not).

My PC is joined to the company’s network, and my user that I have to log in to Windows can access Azure (e.g. the portal and such) – but somehow, when I do this:

var azureDefaultCrendentials = new DefaultAzureCredential();

string uri = "https://*somevaluehere*.vault.azure.net";

SecretClient keyvaultClient = new SecretClient(new Uri(uri), azureDefaultCrendentials);

var clientId = keyvaultClient.GetSecret("ClientId");

The call keyvaultClient.GetSecret(...) fails with an error:

Azure.RequestFailedException
Caller is not authorized to perform action on resource.

which seems to indicate to me that the DefaultAzureCredential isn’t really picking up my logged in Windows / Active Directory user…

I then also found another snippet of code, which I also tried:

string[] scopes = new string[] { "user.read" };

var app = PublicClientApplicationBuilder.Create(myClientId)
    .WithDefaultRedirectUri()
    .Build();

var accounts = app.GetAccountsAsync().Result.ToList();

if (accounts != null && accounts.Any())
{
    string accountNames = string.Join("; ", accounts.Select(a => a.Environment + ": " + a.Username));
    lblUploaded.Text= accountNames;
}

AuthenticationResult result;

try
{
    result = app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                 .ExecuteAsync()
                 .Result;
}
catch (MsalUiRequiredException)
{
    result = app.AcquireTokenInteractive(scopes).ExecuteAsync().Result;
}

But here, I get no results from the app.GetAccountsAsync() call, and the app.AcquireTokenSilent call fails with

System.AggregateException
One or more errors occurred. (No account or login hint was passed to the AcquireTokenSilent call. )

I wish I could pass an account – but I’m not getting any back ….

With all the flood of documentation and samples for Azure, it’s kinda hard to figure out which one is still valid and the way to go, and which others might be deprecated and just cluttering the interwebs….

Any ideas? Takers? Things I can try to do? I’d really love to get this Windows Forms app up and running….

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật