public static async Task <string> GetAuthUrlAsync(ResumptionCookie resumptionCookie, string[] scopes)
        {
            var extraParameters = BuildExtraParameters(resumptionCookie);
            Uri redirectUri     = new Uri(AuthSettings.RedirectUrl);

            if (string.Equals(AuthSettings.Mode, "v2", StringComparison.OrdinalIgnoreCase))
            {
                InMemoryTokenCacheMSAL tokenCache = new InMemoryTokenCacheMSAL();
                Microsoft.Identity.Client.ConfidentialClientApplication client = new Microsoft.Identity.Client.ConfidentialClientApplication("https://login.microsoftonline.com/" + AuthSettings.Tenant + "/oauth2/v2.0",
                                                                                                                                             AuthSettings.ClientId, redirectUri.ToString(),
                                                                                                                                             new Microsoft.Identity.Client.ClientCredential(AuthSettings.ClientSecret),
                                                                                                                                             tokenCache);


                //var uri = "https://login.microsoftonline.com/" + AuthSettings.Tenant + "/oauth2/v2.0/authorize?response_type=code" +
                //    "&client_id=" + AuthSettings.ClientId +
                //    "&client_secret=" + AuthSettings.ClientSecret +
                //    "&redirect_uri=" + HttpUtility.UrlEncode(AuthSettings.RedirectUrl) +
                //    "&scope=" + HttpUtility.UrlEncode("openid profile " + string.Join(" ", scopes)) +
                //    "&state=" + encodedCookie;


                var uri = await client.GetAuthorizationRequestUrlAsync(
                    scopes,
                    null,
                    $"state={extraParameters}");

                return(uri.ToString());
            }
            else if (string.Equals(AuthSettings.Mode, "b2c", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }
            return(null);
        }
Пример #2
0
        public static async Task <string> GetAuthUrlAsync(ResumptionCookie resumptionCookie)
        {
            var encodedCookie = UrlToken.Encode(resumptionCookie);

            Uri redirectUri = new Uri(AuthSettings.RedirectUrl);

            if (string.Equals(AuthSettings.Mode, "v1", StringComparison.OrdinalIgnoreCase))
            {
                Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(AuthSettings.EndpointUrl + "/" + AuthSettings.Tenant);

                var uri = await context.GetAuthorizationRequestUrlAsync(
                    AuthSettings.ResourceId,
                    AuthSettings.ClientId,
                    redirectUri,
                    Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier.AnyUser,
                    "state=" + encodedCookie);

                return(uri.ToString());
            }
            else if (string.Equals(AuthSettings.Mode, "v2", StringComparison.OrdinalIgnoreCase))
            {
                InMemoryTokenCacheMSAL tokenCache = new InMemoryTokenCacheMSAL();

                Microsoft.Identity.Client.ConfidentialClientApplication client = new Microsoft.Identity.Client.ConfidentialClientApplication(AuthSettings.ClientId, redirectUri.ToString(),
                                                                                                                                             new Microsoft.Identity.Client.ClientCredential(AuthSettings.ClientSecret),
                                                                                                                                             tokenCache);

                var uri = await client.GetAuthorizationRequestUrlAsync(
                    AuthSettings.Scopes,
                    null,
                    "state=" + encodedCookie);

                //,
                //    null
                //    clientId.Value,
                //    redirectUri,
                //    Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.UserIdentifier.AnyUser,
                //    "state=" + encodedCookie);

                return(uri.ToString());
            }
            else if (string.Equals(AuthSettings.Mode, "b2c", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }
            return(null);
        }