다음을 통해 공유


TokenAcquirerFactory.GetDefaultInstance Method

Definition

Overloads

GetDefaultInstance(String)

Get the default instance. Use this method to retrieve the instance, optionally add some services to the service collection, and build the instance.

GetDefaultInstance<T>(String)

Get the default instance of a token acquirer factory. Use this method, for instance, if you have an ASP.NET OWIN application and you want to get the default instance of the OwinTokenAcquirerFactory.

GetDefaultInstance(String)

Get the default instance. Use this method to retrieve the instance, optionally add some services to the service collection, and build the instance.

public static Microsoft.Identity.Web.TokenAcquirerFactory GetDefaultInstance (string configSection = "AzureAd");
static member GetDefaultInstance : string -> Microsoft.Identity.Web.TokenAcquirerFactory
Public Shared Function GetDefaultInstance (Optional configSection As String = "AzureAd") As TokenAcquirerFactory

Parameters

configSection
String

Returns

Examples

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Graph;
using Microsoft.Identity.Web;

var tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
tokenAcquirerFactory.Services.AddMicrosoftGraph();
var sp = tokenAcquirerFactory.Build();

var graphServiceClient = sp.GetRequiredService<GraphServiceClient>();
var users = await graphServiceClient.Users
    .Request()
    .WithAppOnly()
    .GetAsync();
Console.WriteLine($"{users.Count} users");

Applies to

GetDefaultInstance<T>(String)

Get the default instance of a token acquirer factory. Use this method, for instance, if you have an ASP.NET OWIN application and you want to get the default instance of the OwinTokenAcquirerFactory.

public static T GetDefaultInstance<T> (string configSection = "AzureAd") where T : Microsoft.Identity.Web.TokenAcquirerFactory, new();
static member GetDefaultInstance : string -> 'T (requires 'T :> Microsoft.Identity.Web.TokenAcquirerFactory and 'T : (new : unit -> 'T))
Public Shared Function GetDefaultInstance(Of T As {TokenAcquirerFactoryNew}) (Optional configSection As String = "AzureAd") As T

Type Parameters

T

Parameters

configSection
String

Returns

T

Examples

using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Owin;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Client;
using Microsoft.Identity.Web.OWIN;

namespace OwinWebApp
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            OwinTokenAcquirerFactory factory = TokenAcquirerFactory.GetDefaultInstance<OwinTokenAcquirerFactory>();

            app.AddMicrosoftIdentityWebApp(factory);
            factory.Services
                .Configure<ConfidentialClientApplicationOptions>(options => { options.RedirectUri = "https://localhost:44386/"; })
                .AddMicrosoftGraph()
                .AddDownstreamApi("DownstreamAPI1", factory.Configuration.GetSection("DownstreamAPI"))
                .AddInMemoryTokenCaches();
            factory.Build();

            /*
            app.AddMicrosoftIdentityWebApp(configureServices: services =>
            {
                services
                .Configure<ConfidentialClientApplicationOptions>(options => { options.RedirectUri = "https://localhost:44386/"; })
                .AddMicrosoftGraph()
               // WE cannot do that today: Configuration is not available.
               // .AddDownstreamApi("CalledApi", null)
                .AddInMemoryTokenCaches();
            });
            */

        }
    }
}

Applies to