Base solution for your next web application
Open Closed

new controller method - get 302 found error and then gets redirect to Login even though i have logged in #6336


User avatar
0
kumaran created

I am using Angular and .net core. I am trying to add a new controller Get method in proj.Web.Core project.

In this method i need to access my logged in current user id so it is a secured method.

When the UI calls this url i get the following Request URL: http://localhost:22742/Profile/GetMyTicketImage?orderId=8011 Request Method: GET Status Code: 302 Found

and then redirects to

Request URL: http://localhost:22742/Account/Login?ReturnUrl=%2FProfile%2FGetMyTicketImage%3ForderId%3D8011 Request Method: GET Status Code: 404 Not Found

What i am missing when i am adding a new controller method?

If it is an anonymous method, it its of no use as i dont get my current user id. I have seen another controller method when you post the profile picture in profile controller which access the current user id and that one works but not mine.

Can anyone help?


8 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Please shared controller your code.

  • User Avatar
    0
    kumaran created

    Here is my code.

    using Abp.Application.Services.Dto;
    using Abp.AspNetCore.Mvc.Authorization;
    using Abp.Runtime.Session;
    using Events.Order;
    using Events.Show;
    using Events.Show.Dtos;
    using Microsoft.AspNetCore.Mvc;
    using System.Threading.Tasks;
    
    namespace Events.Web.Controllers
    {
        
        public class TicketController : EventsControllerBase
        {
            private readonly IShowsAppService _showAppService;
            private readonly OrderManager _orderManager;
    
    
            public TicketController(
                OrderManager orderManager,
                IShowsAppService showAppService)
            {
                _showAppService = showAppService;
                _orderManager = orderManager;
            }
    
            [AbpMvcAuthorize]
            public async Task<ActionResult> GetTicketImage(int orderId)
            {
               long currentUserId = AbpSession.GetUserId();
               Order.Order order = await _orderManager.GetOrderDetails(orderId, currentUserId);
    
                var invalidOrder = "Invalid Order";
                byte[] ticketImagByteArr;
                if (order != null && order.OrderStatus == OrderStatus.Paid)
                {
                    var showForView = await _showAppService.GetShowForView(new EntityDto(order.ShowId));
                    if (showForView != null && showForView.Show != null)
                    {
                        ShowDto show = showForView.Show;
    
                        string qrTicketDetailString = GenerateQrStringFromOrder(order, show);
    
                        ticketImagByteArr = OrderLibrary.GetTicketQR(qrTicketDetailString);
    
                        return File(ticketImagByteArr, "png", "EventOrder-" + orderId);
                    }
    
                }
    
                //render an invalid order qr code.
                ticketImagByteArr = OrderLibrary.GetTicketQR(invalidOrder);
    
                return File(ticketImagByteArr, "png", "EventOrder-Invalid");
            }
    
    
            private static string GenerateQrStringFromOrder(Order.Order order, ShowDto show)
            {
                string output = string.Empty;
    
               ..
               ..
               ..
    
                return output;
    
            }
        }
    }
    
  • User Avatar
    0
    maliming created
    Support Team

    Is it because you requested an incorrect Url?

    GetTicketImage
    GetMyTicketImage
    
  • User Avatar
    0
    kumaran created

    Hi

    Sorry my initial one has no "MyTicketImage" later i added them. It still does not work.

    what i can think of the difference between the profile controller and my controller is in profile controller the uploaderoption is sending the bearer token from the ui to the server.

    In my case i am not sending anything from the UI. Is that the reason? If that is the reason, how i do i send the token. Mine is just a image url that renders the image by calling a controller method.

    Thanks

  • User Avatar
    0
    maliming created
    Support Team

    Can you provide a complete code that allows me to reproduce the problem you are experiencing?

  • User Avatar
    0
    kumaran created

    You should be kidding me. You want me to create a mini project just to reproduce the error for you? I cannot send you my entire project.

  • User Avatar
    0
    maliming created
    Support Team

    No need for a complete project. Just code that can reproduce the problem.

  • User Avatar
    1
    kumaran created

    Hi

    Just to let you know that i have solved my problem. This will help others who have the same issue.

    The issue is my request to my controller method is in a image url where i get the image as byte array. Because of this the UI layer is not attaching the bearer token. As a workaround i moved the method that returns the array to the service layer and called through the proxy.

    In order to render the image with the byte array i followed the way it is done in this link. http://www.macaalay.com/2014/09/26/rendering-images-from-byte-arrays-and-converting-images-to-byte-arrays-using-angularjs/