Note that there are some explanatory texts on larger screens.

plurals
  1. POIE looping infinitely when using Authorize
    primarykey
    data
    text
    <p>I'm developing a Facebook app, and i only want to allow access to certain views if the visitor is authorized through Facebook. This should be a pretty simple task, and i thought is was, until i tried it out in IE. The following code works fine in Chrome and Safari. I want to use Forms authentication, and therefore i have set</p> <pre><code>&lt;forms loginUrl="~/Account/Login" timeout="2880" /&gt; </code></pre> <p>in web.config. This will direct the visitor to the following ActionResult when entering my app:</p> <pre><code> public ActionResult Login(string returnUrl) { ManagerGame2.Utilities.StaticDataContent.InitStaticData(); var oAuthClient = new FacebookOAuthClient(); oAuthClient.AppId = FacebookApplication.Current.AppId; oAuthClient.RedirectUri = new Uri(redirectUrl); var loginUri = oAuthClient.GetLoginUrl(new Dictionary&lt;string, object&gt; { { "state", returnUrl } }); return Redirect(loginUri.AbsoluteUri); } </code></pre> <p>Then the user is redirected to a Facebook page, and an access token is sent back into my OAuth ActionResult:</p> <pre><code>public ActionResult OAuth(string code, string state) { FacebookOAuthResult oauthResult; if (FacebookOAuthResult.TryParse(Request.Url, out oauthResult)) { if (oauthResult.IsSuccess) { var oAuthClient = new FacebookOAuthClient(); oAuthClient.AppId = FacebookApplication.Current.AppId; oAuthClient.AppSecret = FacebookApplication.Current.AppSecret; oAuthClient.RedirectUri = new Uri(redirectUrl); dynamic tokenResult = oAuthClient.ExchangeCodeForAccessToken(code); string accessToken = tokenResult.access_token; DateTime expiresOn = DateTime.MaxValue; if (tokenResult.ContainsKey("expires")) { DateTimeConvertor.FromUnixTime(tokenResult.expires); } FacebookClient fbClient = new FacebookClient(accessToken); dynamic me = fbClient.Get("me?fields=id,name"); long facebookID = Convert.ToInt64(me.id); Account acc = (from x in db.Account.OfType&lt;Account&gt;() where x.FaceBookID == facebookID select x).FirstOrDefault(); if (acc == null) { acc = CreateAccount(me); } acc.LatestLogin = DateTime.Now; db.Entry(acc).State = EntityState.Modified; db.SaveChanges(); MemoryUserStore.CurrentAccount = acc; UserRoleProvider usp = new UserRoleProvider(); usp.GetRolesForUser(acc.AccountID.ToString()); FormsAuthentication.SetAuthCookie(acc.AccountID.ToString(), false); if (Url.IsLocalUrl(state)) { return Redirect(state); } return RedirectToAction("Details", "Account", new { id = acc.AccountID }); } } return RedirectToAction("Index", "Account"); } </code></pre> <p>What i am trying to do here, is to first verify if the token i get back from the redirect is valid. If it is, then i pull some data about the visitor, like FacebookID and Name. I then match it with my database, to see if the user already exists, and if not, i create one. I also assign a role for the user in my custom Role provider, but i had the infinite loop problem before this. Then i set</p> <pre><code>FormsAuthentication.SetAuthCookie(acc.AccountID.ToString(), false); </code></pre> <p>and i assume this is the core of keeping track of wheter a visitor is authorized or not. As far as i understand, when the visitor is trying to call a ActionResult that requires [Authorize] then the system will check for this cookie.</p> <p>Well, could someone please clarify why the above code is working in Chrome/Safari, but keeps looping through Login and then OAuth infinitely in IE?</p> <p>My app is using MVC 3, EF Code First and Facebook C# SDK 5.0.25</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload