简体   繁体   English

具有多个控制器的Facebook ASP.NET MVC App

[英]Facebook ASP.NET MVC App with multiple controllers

I am using the Facebook C# SDK to develop an iframe Facebook application. 我正在使用Facebook C#SDK开发iframe Facebook应用程序。

I looked at the example and find this piece of code to do authorization in a controller: 我看了一下示例,发现这段代码可以在控制器中进行授权:

namespace Auth_And_Allow.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        [CanvasAuthorize(Perms = "user_about_me")]
        public ActionResult Index()
        {
            FacebookApp fbApp = new FacebookApp();
            if (fbApp.Session != null)
            {
                dynamic result = fbApp.Get("me");

                ViewData["Firstname"] = result.first_name;
                ViewData["Lastname"] = result.last_name;
            }

            return View();
        }
    }
}

But what should i do if my app is using a lot more then one controller? 但是,如果我的应用程序使用的控制器多于一个控制器,该怎么办?

Should i use the same authorization code in all controllers or is there another way? 我应该在所有控制器中使用相同的授权代码,还是有其他方法? (I know it will work that way but right now i am searching for best practices to build facebook apps) (我知道它会那样工作,但现在我正在寻找构建Facebook应用程序的最佳实践)

The CanvasAuthorize attribute will ensure that your user is logged in and has the appropriate permissions. CanvasAuthorize属性将确保您的用户已登录并具有适当的权限。 You dont need to check this again by checking if the Session is null. 您无需通过检查Session是否为null来再次检查。 Additionally, the CanvasAuthorize attribute (like the regular Authorize attribute) can be applied to you controllers as well as your actions. 此外,CanvasAuthorize属性(如常规的Authorize属性一样)可以应用于您的控制器以及您的操作。 I would just do something like this: 我会做这样的事情:

[CanvasAuthorize(Perms = "user_about_me")]
public class FirstController : Controller {


}

[CanvasAuthorize(Perms = "user_about_me")]
public class SecondController : Controller {

}

Make sure you use the Controller extensions named CanvasRedirect accessed by this.CanvasRedirect inside a controller with the Facebook.Web.Mvc namespace referenced. 确保在引用了Facebook.Web.Mvc命名空间的控制器内使用由this.CanvasRedirect访问的,名为CanvasRedirect的Controller扩展。 These redirect helpers will ensure that you redirect correctly and dont "lose" the user's session. 这些重定向助手将确保您正确重定向,并且不会“丢失”用户的会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM