0

I have some data that I need to get after redirecting my URL to yahoo auth for authentication and access token. I tried using Session and tempData, but both get cleared after redirection and callback to another ActionMethod. Tried using HttpCookie too but it doesn't retain the value either. How do I store this value and get it after redirection to callback function? Whatever I tried, I get null value. It gets saved at first but gets erased after redirection.

public async Task<ActionResult> YahooAuth(int Id)
    {
        List<DataAccess.event_dates> yahooEvents = _iadminSettingsService.GetEventDatesByEventId(Id);
        Session["yahooEventObj"] = yahooEvents;
        TempData["yahoEvnts"] = yahooEvents;
        System.Web.HttpCookie cookie = new System.Web.HttpCookie("eventID", Id.ToString());
        Response.Cookies.Add(cookie);
        var url = "https://api.login.yahoo.com/oauth2/request_auth?client_id=XXX&redirect_uri=https://b0552ca5.ngrok.io/Event/YahooCalendar&response_type=code&language=en-us";
        return Redirect(url);
    }

[HttpGet]
    public async Task<ActionResult> YahooCalendar(string code)
    {
        List<DataAccess.event_dates> yahooEvents = (List<DataAccess.event_dates>)Session["yahooEventObj"];
        List<DataAccess.event_dates> lst = (List<DataAccess.event_dates>)TempData["yahoEvnts"];
        string Id = Request.Cookies["eventID"].ToString();
        List<DataAccess.event_dates> yahooEvents = _iadminSettingsService.GetEventDatesByEventId(Convert.ToInt16(Id));
        . . .

        return Redirect("https://calendar.yahoo.com/");
    }
  • Do you have the method bodies the wrong way around? I mean that `YahooCalendar` seems to deal with auth, and `YahooAuth` seems to deal with the calendar. – ProgrammingLlama Nov 28 '18 at 06:45
  • @John yeah, sorry that's wrong naming. edited it. do you know any solution for retaining the value after redirection? thanks. – Sanjivni Rana Nov 28 '18 at 06:48
  • @MintOp Cookies should work, but I don't see you setting the "Expires" value, which could mean you invalidate the cookie almost immediately after setting it. – zuckerburg Nov 28 '18 at 06:58
  • @zuckerburg it's not working even after setting the cookie expire time, i get null – Sanjivni Rana Nov 28 '18 at 07:28
  • Can you confirm with DevTools to see if the cookie has been written at all? The only thing I can think of is that the domain the cookie is set on, is different to the one requesting to read it, resulting in a null value – zuckerburg Nov 28 '18 at 08:28
  • @zuckerburg the cookie is getting stored in the first place but i guess it is getting cleared because i am redirecting to another domain i.e ngrok . – Sanjivni Rana Nov 28 '18 at 10:29
  • @MintOp That is impossible UNLESS YOU clear the cookie yourself at some point, or if the domain is different to the domain that set the cookie. For example, x.com but then come back to calendar.x.com, OR the expiry means it expires as soon as you navigate off the page. If that was the case, the Google overlords wouldn't have the ability to track our every move.... so I highly recommend not redirecting for now and checking your dev tools for more information on expiry etc. Because if it IS set, something else is wiping it (perhaps your local browser is set not to remember any cookies) etc. – zuckerburg Nov 28 '18 at 11:01
  • 1
    @zuckerburg yeah i get it. thanks a lot. much appreciated. – Sanjivni Rana Nov 28 '18 at 11:27

1 Answers1

0

In my opinion all method by Session, Tempdata and Cookies can work fine.

I check your code and found you are using ngrock for localhost redirection.

please make sure when you start your application if it's hosting with http://localhost:port and after redirection if it's with ngRock domain name then any of method not working

Session, Tempdata and Cookies store by domain name

please check with application starting with ngRock domain and check after redirection you get data or not?

May this help you.

Thanks

Ajay
  • 66
  • 4
  • when I start the app with ngrok I am able to get the value using all three methods. but how should I retain this value when domain changes to another one? – Sanjivni Rana Nov 28 '18 at 10:41
  • You can't share session between two domain, But if you want then you can save data on database at any unique id and pass this id to call back URL. After callback get data by unique id Also there Single sign on technique for session share but need more efforts to implements. and it's issue in localhost. When you publish website i think you have same domain after redirection so no problem at that time. – Ajay Nov 28 '18 at 11:02
  • @MintOp You can't set a cookie for a domain you do not control. You can only set a cookie for yourself. If the person navigates to a different site or you redirect them to a different site, as soon as they come back to your site, you can access that cookie as they're on your domain. They cannot access a cookie you set for domain x.com but then try to read it from calender.yahoooo.com or the like. – zuckerburg Nov 28 '18 at 11:03
  • @Ajay thanks a lot. yes you're right, there will be no problem after publishing. I can manage to test it on local using one domain. thanks a lot for the help. much appreciated. – Sanjivni Rana Nov 28 '18 at 11:29