xamarin / SalesforceSDK

Salesforce SDK component
MIT License
31 stars 15 forks source link

"System.InvalidCastException: Specified cast is not valid." #30

Open slajalin opened 7 years ago

slajalin commented 7 years ago

Hello!

We're currently trying to setup Salesforce authentication in iOS using SalesforceClient. We had set it up such that we're using a Sandbox authentication path with a callback path to go back into the app.

var consumerId = [*consumerId];
var consumerSecret = [*consumerSecret];
var callbackUri = [*deeplink to get back into the app];

SalesforceClient.AuthPath = [*sandbox path];
 _salesforceClient = new SalesforceClient(consumerId, consumerSecret, callbackUri);
 _salesforceClient.AuthenticationComplete += (sender, e) => AuthenticationCompleted(e);

The login works fine until the user taps on the 'Allow' button in the 'Allow Access?' prompt and the app throws a 'Specified cast is no valid error' within the OnAuthenciationCompleted method.

Below is the log:

System.InvalidCastException: Specified cast is not valid. at Salesforce.SalesforceClient.OnAuthenticationCompleted (System.Object sender, Xamarin.Auth.AuthenticatorCompletedEventArgs args) [0x00012] in <6e169f97be3646f7a85d849a89ea6c50>:0 at Xamarin.Auth.Authenticator+cAnonStorey0.<>m0 () [0x00024] in <6a2e0651de6d464db7ddb11baf25b4b5>:0 at Xamarin.Auth.Authenticator+cAnonStorey2.<>m0 () [0x00000] in <6a2e0651de6d464db7ddb11baf25b4b5>:0 at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/Foundation/NSAction.cs:163 at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:63 at TestApplication.iOS.Application.Main (System.String[] args) [0x00001] in /Users/[myuser]/repos/TestApplication/iOS/Main.cs:17

Is there any other setup that could be missing or have you seen this issue before? Any insight on this would be greatly appreciated! Thanks.

kcreech101 commented 6 years ago

This is happening to me as well, any Luck?

jimmynguyen17 commented 6 years ago

I'm getting the same thing when running from Android. It appears to come from SalesforceClient.cs in its OnAuthenticationCompleted method:

CurrentUser = (ISalesforceUser)args.Account

args.Account is of type Account as defined in the external Xamarin.Auth library, while CurrentUser is of type ISalesforceUser which is defined in the Salesforce.Android library of the SalesforceSDK. The Account class does not derive from ISalesforceUser, therefore this throws an exception as an invalid cast.

I was able to create a workaround for this particular line of code, however there are numerous other instances of ISalesforceUser and Account being cast back and forth which throw similar exceptions. Does anyone have a solution for this?

craigathon commented 6 years ago

@jimmynguyen17 I only ran into this error when creating a custom build. I couldn't find a way to cast to ISalesforceUser correctly so I just ripped it out along with SalesforceUser. I now use the Xamarin.Auth.Account class throughout the project instead with the RequiresReauthentication property added.