예제 #1
0
        /// <summary>
        /// Logs the user into Office 365.
        /// </summary>
        /// <param name="authState">The login or logout status of the user.</param>
        /// <returns>A redirect to the Office 365 login page.</returns>
        public async Task <ActionResult> Login(string authState)
        {
            if (string.IsNullOrEmpty(Settings.AzureADClientId) || string.IsNullOrEmpty(Settings.AzureADClientSecret))
            {
                ViewBag.Message = "Please set your client ID and client secret in the Web.config file";
                return(View());
            }

            // TODO: ADDED withAuth
            ConfidentialClientApplicationBuilder clientBuilder = ConfidentialClientApplicationBuilder.Create(Settings.AzureADClientId)
                                                                 .WithAuthority(Settings.AzureADAuthority)
                                                                 .WithClientSecret(Settings.AzureADClientSecret);

            ConfidentialClientApplication clientApp = (ConfidentialClientApplication)clientBuilder.Build();

            // Generate the parameterized URL for Azure login.
            string[] graphScopes = { "Files.Read.All", "User.Read" };
            var      urlBuilder  = clientApp.GetAuthorizationRequestUrl(graphScopes);

            urlBuilder.WithRedirectUri(loginRedirectUri.ToString());
            urlBuilder.WithAuthority(Settings.AzureADAuthority);
            urlBuilder.WithExtraQueryParameters("state=" + authState);
            var authUrl = await urlBuilder.ExecuteAsync(System.Threading.CancellationToken.None);

            // Redirect the browser to the login page, then come back to the Authorize method below.
            return(Redirect(authUrl.ToString()));
        }
        public async Task <ActionResult> Login()
        {
            if (string.IsNullOrEmpty(_azureAdOptions.ClientId) || string.IsNullOrEmpty(_azureAdOptions.ClientSecret))
            {
                ViewBag.Message = "Please set your client ID and client secret in the Web.config file";
                return(View());
            }

            ConfidentialClientApplicationBuilder clientBuilder = ConfidentialClientApplicationBuilder.Create(_azureAdOptions.ClientId);
            ConfidentialClientApplication        clientApp     = (ConfidentialClientApplication)clientBuilder.Build();

            string[] graphScopes = { "profile" };
            var      urlBuilder  = clientApp.GetAuthorizationRequestUrl(graphScopes);

            urlBuilder.WithRedirectUri(LoginRedirectUri.ToString());
            urlBuilder.WithAuthority(_azureAdOptions.Authority);


            var authUrl = await urlBuilder.ExecuteAsync(System.Threading.CancellationToken.None);

            return(Redirect(authUrl.ToString()));
        }