问题
I have researched about the topic mentioned in the title. Solutions say (besides this one) that the "Precondition failed: !string.IsNullOrEmpty(settings.AppId)"
exception thrown by FacebookWebContext.IsAuthorized()
is caused by not setting the signed request, which can be got from the query string.
I'm developing an ASP.NET MVC website in what I would like to implement Facebook Connect authentication. The authentication part works fine, but if I try to check the permissions the user gave the app (either with HasPermissions()
or IsAuthorized()
) I get this error.
FacebookWebContext
gets properly initialized with app settings. And as far as I know signed_request
is only available in canvas apps, or am I wrong?
How can one check the permissions in an ordinary website after the user has authenticated?
Thanks in advance!
Edit: Here are the details of the exception that's thrown:
System.Diagnostics.Contracts.__ContractsRuntime.ContractException occurred Message=Precondition failed: !string.IsNullOrEmpty(settings.AppId) Source=Facebook.Web StackTrace:
at System.Diagnostics.Contracts.__ContractsRuntime.TriggerFailure(ContractFailureKind kind, String message, String userMessage, String conditionText, Exception inner)
at System.Diagnostics.Contracts.__ContractsRuntime.ReportFailure(ContractFailureKind kind, String message, String conditionText, Exception inner)
at System.Diagnostics.Contracts.__ContractsRuntime.Requires(Boolean condition, String message, String conditionText)
at Facebook.Web.FacebookWebContext..ctor(IFacebookApplication settings, HttpContextBase httpContext)
at Facebook.Web.FacebookWebContext..ctor()
at Facebook.Web.FacebookWebContext.get_Current()
at Facebook.Web.FacebookWebClient..ctor(String accessToken)
at Facebook.Web.FacebookWebContext.HasPermissions(String accessToken, String appId, Int64 userId, String[] permissions)
at Facebook.Web.FacebookWebContext.IsAuthorized(String[] permissions)
at Piedone.FacebookTest.IsAuthorized(String[] permissions) InnerException:
回答1:
make sure to set the facebook settings in web.config
<configSections>
<section type="Facebook.FacebookConfigurationSection, Facebook" name="facebookSettings" allowLocation="true" allowDefinition="Everywhere" />
</configSections>
<facebookSettings appId = "{app_id}" appSecret = "{app_secret}"/>
The sdk needs to know the app id and app secret inorder to extract information set by the Facebook Javascript SDK.
You might also want to check out the "Samples" folder in the source code.
来源:https://stackoverflow.com/questions/7349362/facebook-c-sharp-sdk-signed-request-is-null-in-isauthorized