Sign in html page returns while web api call instead of data

Viewed 31

result for endpoint call return sign in page I have a .net mvc web app which deployed on azure that has a cookie authentication i need to use the web app endpoint from my another bot .node js project so i have given bearer token in authorization header in get call but in response that returns sign in html page but i need data

configureAuth

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseKentorOwinCookieSaver();

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthenticationPatched(      
            new OpenIdConnectAuthenticationPatchedOptions()
            {
                
                NonceThreshold = 1,
                ClientId = clientId,
                Authority = Authority,
                Scope = $"openid email profile offline_access {graphScopes}",
                PostLogoutRedirectUri = postLogoutRedirectUri,
                TokenValidationParameters = new TokenValidationParameters
                {ValidateIssuer = true,                                             RoleClaimType = System.Security.Claims.ClaimTypes.Role,    
                },
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    RedirectToIdentityProvider = (context) =>
                    {
                        switch (context.ProtocolMessage.RequestType)
                        {
                            case OpenIdConnectRequestType.Authentication:
                                
                                context.OwinContext.Authentication.SignOut("Cookies");
                                break;
                        }
                        string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;
                        context.ProtocolMessage.RedirectUri = appBaseUrl;
                        context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl;
                        //context.ProtocolMessage.Prompt = "consent";
                        return Task.FromResult(0);
                    },
                    AuthenticationFailed = OnAuthenticationFailedAsync,
                    AuthorizationCodeReceived = OnAuthorizationCodeReceivedAsync}

OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification) { var signedInUserId = notification.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; var signedInUserUpn = notification.AuthenticationTicket.Identity.FindFirst(ClaimTypes.Upn).Value;

        var tokenStore = new SessionTokenStore(signedInUserId, signedInUserUpn,
            notification.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase);

        string appBaseUrl = notification.OwinContext.Request.Scheme + "://" + notification.OwinContext.Request.Host + notification.OwinContext.Request.PathBase;

 var idClient = new ConfidentialClientApplication(clientId, appBaseUrl, new ClientCredential(appKey), tokenStore.GetMsalCacheInstance(), null);

        string message;
        string debug = null;

        try
        {
            string[] scopes = graphScopes.Split(' ');

            var result = await idClient.AcquireTokenByAuthorizationCodeAsync(
                notification.Code, scopes);

            var userDetails = await GraphHelper.GetUserDetailsWithTokenAsync(result.AccessToken);

            string email = string.IsNullOrEmpty(userDetails.Mail) ?
                userDetails.UserPrincipalName : userDetails.Mail;

            var userProfilePhoto = await GraphHelper.GetUserProfilePhotoAsync(result.AccessToken);

            var cachedUser = new CachedUser()
            {
                DisplayName = userDetails.DisplayName,
                Email = email,
                UserId = signedInUserId,
                UserUpn = userDetails.UserPrincipalName,
                Avatar = string.Empty
            };
            if (userProfilePhoto != null && userProfilePhoto.Length > 0)
            {
                cachedUser.Avatar = Convert.ToBase64String(userProfilePhoto);
            }
            tokenStore.SaveUserDetails(cachedUser);

}

then i try call from bot app :

 const employees = await fetch("https://localhost:44381/Points/FetchList",{
    method:"GET",
    headers:{
      'Content-Type':'application/text',
      'Authorization': `Bearer ${accessToken}`
    },
    agent:agent
  });
  const details = await employees.text();

am getting html sign in page with 200 status code

0 Answers
Related