!Auth.IsAuthorized method

Mar 10, 2012 at 7:32 PM
Edited Mar 10, 2012 at 7:33 PM
Right now when someone clicks on my page it automatically redirects you to twitter to login. But I rather have it display the public time line.
If the user is registered already then it can load thier timeline.

I am trying to use the if !Auth.IsAuthorized errors. This will help me learn how to display or not display a login label too in the future.

Here is my code

im not getting either to work.

Its not getting passed !Auth.IsAuthorized



        public ActionResult Index()
        {
            if (!Auth.IsAuthorized == true)
            {
               twitterCtx = new TwitterContext();

                var publictweets =
                    (from tweet in twitterCtx.Status
                     where tweet.Type == StatusType.Public
                     select new TweetViewModel
                     {
                         ImageUrl = tweet.User.ProfileImageUrl,
                         ScreenName = tweet.User.Name,
                         Tweet = tweet.Text
                     })
                      .ToList();
              
                return View(publictweets);
            }
            else
            {
                //if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
                //{
                //    credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                //    credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
                //}
                //authToken = Request.QueryString[credentials.OAuthToken];
                //accessToken = Request.QueryString[credentials.AccessToken];

                //Auth = new MvcAuthorizer
                //{
                //    Credentials = credentials
                //};

                //Auth.CompleteAuthorization(Request.Url);

                //if (!Auth.IsAuthorized)
                //{
                //    Uri specialuri = new Uri(Request.Url.ToString());
                //    return Auth.BeginAuthorization(specialuri);
                //}

                if (Request.Cookies == null)
                {
                    Setcookie(credentials.OAuthToken, credentials.AccessToken);
                }

                twitterCtx = new TwitterContext(Auth);

                var friendstweets =
                    (from tweet in twitterCtx.Status
                     where tweet.Type == StatusType.Friends
                     select new TweetViewModel
                     {
                         ImageUrl = tweet.User.ProfileImageUrl,
                         ScreenName = tweet.User.Name,
                         Tweet = tweet.Text
                     })
                      .ToList();

                return View(friendstweets);
            }
        }
Coordinator
Mar 11, 2012 at 7:07 PM

Hi,

It might be an issue with how you're managing the Auth instance.  In Web apps, you have to rebuild your object every time the controller is called, which is why you have to either have credentials available or determine that they aren't available and you need to initiate authentication.  When you say it doesn't work, you'll have to describe what you mean by not working.  I.e. show an error message or a stack trace.

Joe

Mar 11, 2012 at 8:58 PM
Edited Mar 11, 2012 at 9:08 PM

Let me ask this question a different way.  Webforms has a signin authorizer and uses the multiview panels which mvc does not have.

I am in need  of a way to detect if the user is either authorized or not authorized and I can do an action or something.

 

The code above is like the sample, once you load it, it shoots to twitter automatically calling the redirects. I was trying to split it up into 2 , yes we see you are new, here is the public timeline vs  Here is your authenticated timeline.

 

I want to be able to put multiple parts of my web application where an authorized user gets "A" but an Unauthorized gets "b"

 

Like the Display a twitter login if they need to register vs's a Hello Auth.ScreenName if they are signed in.

I was hoping to  put all the code to register and login in like mvc 4 demo does then carry the credentials around if I have them and use auth.IsAuthorized or !auth.authorized

 

I am backtracking a bit right now working with the search but  I think I need to put all the crediential code in the controller code above the action so it can detect or reject  and query at will so I dont keep hitting the api causing a rate limit issue.

 

I also want to mention, I wasnt reading my cookies correctly. Today I have a read and write working. YAY!

Coordinator
Mar 11, 2012 at 9:20 PM
Edited Mar 11, 2012 at 9:21 PM

For any page that you need to interact with Twitter, you'll have code that re-builds the authorizer with credentials.  This is necessary with Web applications because you don't have state between requests, so the authorizer that you were using on the previous request no longer exists on subsequent requests.

If the authoriizer is not given all the credentials when you build it, IsAuthorized will be false and you'll need to authenticate with Twitter.

If you do have all the credentials (which your code keeps track of), your code will load those credentials into a new instance of an authorizer.  Then you can pass the authorizer instance to a new instance of TwitterContext. Once you have that, then you can query Twitter.

Joe