0

I have a .Net Core 3.1 MVC app hosted as Azure Web App and enabled Express Authentication. Now in the code, I want the Azure AD Object ID (in Controller/View). In all the examples and samples, I see multiple ways to get information about User details like Name etc. but could not find anything about getting the Object ID. How to get the same?

My Controller is pretty simple, like this:

public IActionResult Index()
{
    return View();
}

In View, index.cshtml is also very generic.

In the startup.cs, I have:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Please let me know a way to get the Azure AD Object ID in the Index() method in the Controller.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tanmayghosh2507
  • 773
  • 3
  • 12
  • 31

1 Answers1

0

I believe the objectid is being passed as a oid claim. It is part of the id_token you get in Express Authentication.

How do I get an OID claim in ASPCore from Azure B2C

http://schemas.microsoft.com/identity/claims/objectidentifier

User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • I did some research on it. Looks like it should return me the oid as intended, however I am getting null. Is there any config I need to set up in Azure, or what am I missing? – tanmayghosh2507 Jul 19 '20 at 23:01
  • I could get the user's objectID in .cshtml by calling the method `@User.GetObjectId()`. I verified it in the sample at this [line](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/fab815ee50017d0009ce4d699547e7721ceaa248/2-WebApp-graph-user/2-3-Multi-Tenant/Views/Shared/_LoginPartial.cshtml#L6). – Dhivya G - MSFT Identity Aug 25 '20 at 01:46