Ejemplo n.º 1
0
        public async Task <IActionResult> B2C(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            var uri = await confidentialClientApplication.GetAuthorizationRequestUrl(authorizationService.AuthorizationScopes).ExecuteAsync();

            return(new RedirectResult(uri.AbsoluteUri));
        }
Ejemplo n.º 2
0
        public void button1_Click(object sender, EventArgs e)
        {
            string authurl = null;


            try
            {
                //Getting Authorization URL
                //Instantiating and adding properties to Confidential Client
                url = String.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", Domain.Text);
                app = ConfidentialClientApplicationBuilder.Create(ClientID.Text).WithClientSecret(Secret.Text).WithRedirectUri(redirectURI.Text).Build();
                var GeneratedURL = app.GetAuthorizationRequestUrl(scopes).WithAuthority(url, true).ExecuteAsync();
                GeneratedURL.Wait();
                authurl = GeneratedURL.Result.AbsoluteUri;
                Logger.WriteLog("Generated Authorization URL successfully: " + authurl);
            }
            catch (Microsoft.Identity.Client.MsalClientException eexc)
            {
                Logger.WriteLog("Issue encountered while generating Authorization URL: " + eexc);
                throw;
            }
            catch (System.AggregateException eexc)
            {
                Logger.WriteLog("Issue encountered while generating Authorization URL. Please check that your domain is entered correctly: " + eexc);
                throw;
            }

            //Navigating to Authorization URL
            Form2 popup = new Form2(authurl, redirectURI.Text);

            popup.ShowDialog(this);
            AuthorizationCodeform = popup.Code;
            Logger.WriteLog("Authorization Code obtained succesfully: " + AuthorizationCodeform);



            try
            {
                //Generating Access token
                url = String.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", Domain.Text);

                var getToken = app.AcquireTokenByAuthorizationCode(scopes, AuthorizationCodeform).WithAuthority(url, true).ExecuteAsync();
                getToken.Wait();
                accnt = getToken.Result.Account;
                string at = getToken.Result.AccessToken;
                accessToken.Text = at;
                Logger.WriteLog("Access Token obtained succesfully: " + at);
            }
            catch //Catch is generic due to possible network issues
            {
                Logger.WriteLog("Issue encountered while obtaining Authrization Code. Please check the AAD Log-in prompt error for more details. This log was generated due to empty Authorization Code");

                throw;
            }
        }
Ejemplo n.º 3
0
        public void EnsurePublicApiSurfaceExistsOnInterface()
        {
            IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(TestConstants.ClientId)
                                                 .WithClientSecret("cats")
                                                 .Build();

            // This test is to ensure that the methods we want/need on the IConfidentialClientApplication exist and compile.  This isn't testing functionality, that's done elsewhere.
            // It's solely to ensure we know that the methods we want/need are available where we expect them since we tend to do most testing on the concrete types.

            var authCodeBuilder = app.AcquireTokenByAuthorizationCode(TestConstants.s_scope, "authorizationcode");

            PublicClientApplicationTests.CheckBuilderCommonMethods(authCodeBuilder);

            var clientBuilder = app.AcquireTokenForClient(TestConstants.s_scope)
                                .WithForceRefresh(true)
                                .WithSendX5C(true);

            PublicClientApplicationTests.CheckBuilderCommonMethods(clientBuilder);

            var onBehalfOfBuilder = app.AcquireTokenOnBehalfOf(
                TestConstants.s_scope,
                new UserAssertion("assertion", "assertiontype"))
                                    .WithSendX5C(true);

            PublicClientApplicationTests.CheckBuilderCommonMethods(onBehalfOfBuilder);

            var silentBuilder = app.AcquireTokenSilent(TestConstants.s_scope, "*****@*****.**")
                                .WithForceRefresh(false);

            PublicClientApplicationTests.CheckBuilderCommonMethods(silentBuilder);

            silentBuilder = app.AcquireTokenSilent(TestConstants.s_scope, TestConstants.s_user)
                            .WithForceRefresh(true);
            PublicClientApplicationTests.CheckBuilderCommonMethods(silentBuilder);

            var requestUrlBuilder = app.GetAuthorizationRequestUrl(TestConstants.s_scope)
                                    .WithAccount(TestConstants.s_user)
                                    .WithLoginHint("loginhint")
                                    .WithExtraScopesToConsent(TestConstants.s_scope)
                                    .WithRedirectUri(TestConstants.RedirectUri);

            PublicClientApplicationTests.CheckBuilderCommonMethods(requestUrlBuilder);

            var byRefreshTokenBuilder = ((IByRefreshToken)app).AcquireTokenByRefreshToken(TestConstants.s_scope, "refreshtoken")
                                        .WithRefreshToken("refreshtoken");

            PublicClientApplicationTests.CheckBuilderCommonMethods(byRefreshTokenBuilder);
        }
        public static async Task <string> GenerateAuthorizationRequestUrl(string[] scopes, IConfidentialClientApplication cca, HttpContextBase httpcontext, UrlHelper url)
        {
            string signedInUserID          = ClaimsPrincipal.Current.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value;
            string preferredUsername       = ClaimsPrincipal.Current.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn").Value;
            Uri    oauthCodeProcessingPath = new Uri(httpcontext.Request.Url.GetLeftPart(UriPartial.Authority).ToString());
            string state = GenerateState(httpcontext.Request.Url.ToString(), httpcontext, url, scopes);

            Uri authzMessageUri = await cca
                                  .GetAuthorizationRequestUrl(scopes)
                                  .WithRedirectUri(oauthCodeProcessingPath.ToString())
                                  .WithLoginHint(preferredUsername)
                                  .WithExtraQueryParameters(state == null ? null : "&state=" + state)
                                  .WithAdfsAuthority(cca.Authority)
                                  .ExecuteAsync(CancellationToken.None)
                                  .ConfigureAwait(false);

            return(authzMessageUri.ToString());
        }
Ejemplo n.º 5
0
        public static async Task<string> GenerateAuthorizationRequestUrl(string[] scopes, IConfidentialClientApplication cca, HttpContextBase httpcontext, UrlHelper url)
        {
            string signedInUserID = ClaimsPrincipal.Current.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value;
            string preferredUsername = ClaimsPrincipal.Current.FindFirst("preferred_username").Value;
            Uri oauthCodeProcessingPath = new Uri(httpcontext.Request.Url.GetLeftPart(UriPartial.Authority).ToString());
            string state = GenerateState(httpcontext.Request.Url.ToString(), httpcontext, url, scopes);
            string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;

            string domain_hint = (tenantID == ConsumerTenantId) ? "consumers" : "organizations";

            Uri authzMessageUri = await cca
                .GetAuthorizationRequestUrl(scopes)
                .WithRedirectUri(oauthCodeProcessingPath.ToString())
                .WithLoginHint(preferredUsername)
                .WithExtraQueryParameters(state == null ? null : "&state=" + state + "&domain_hint=" + domain_hint)
                .WithAuthority(cca.Authority)
                .ExecuteAsync(CancellationToken.None)
                .ConfigureAwait(false);

            return authzMessageUri.ToString();
        }