Reputation: 9229
I have a constants class in my C# project. I am using a Constants
class, which holds values which I am using to configure a OAuth Login Call. Whether someone clicks "Login with Google" or "Login with Facebook" etc. defines what value these public constants should be.
I am a self taught programmer, so can see that I might be likely to do something here which can cause issues. So how are you supposed to handle this.
Here is what I have at the moment, but I would like to have just APP_ID
, which would have a value dependant on which login button was pressed.
I could set these in a sub routine, but I am not sure if that is best practice?
public class Constants
{
public const string FB_APP_ID = "xxxxxx";
public const string FB_SCOPE = "";
public const string FB_APP_NAME = "xxxx";
public const string AUTH_URI = "https://m.facebook.com/dialog/oauth/";
public const string RETURN_URI = "https://www.facebook.com/connect/login_success.html";
public const string G_APP_ID = "yyyyyyy.apps.googleusercontent.com";
public const string S_SCOPE = "https://www.googleapis.com/auth/userinfo.email";
}
Just to note a few points about my set up. I am developing using Xamarin for cross platform mobile development, this isn't very relevant. However, it does mean that my solution has 3 projects, a Portable Class Library, an iOS project and an Android one. So I have some buttons described on a view page in my PCL, with some Button.Clicked
handlers behind them. This is where I would like to set the value of my global OAuth details class. I then have to reference this OAuth instance from my iOS and Android projects, whether it is a Google, Facebook or other set of values.
Upvotes: 1
Views: 1046
Reputation: 31696
Create an app.config
file to hold these so called constants. The config fill will be brought over with the application and read during run-time. That way the config can be changed by the user(?) if needed without re-compiling the program.
See
Then you can add an appSettings
node to the app.config
file to hold all the application values, here is one:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="TargetURL" value="https://Omegacoder.Com" />
</appSettings>
</configuration/>
And extract as such (if using C# 6 found in Visual Studio 2015, otherwise set the property in code by call ConfigurationManager
and assigning it that way):
public static string TargetUrl { get; set; } =
ConfigurationManager.AppSettings["TargetURL"];
Note I put my constants into statics, but one can simply call ConfigurationManger
at anytime in your program and does not have to assign the value as shown in my example.
Update
At this time the Xamarin projects do not support application configuration files.
Upvotes: 1
Reputation: 7934
Following on from my comment about configuration...
In a typical Windows/Web .NET application you would use the ConfigurationManager
classes with custom configuration sections to do the gruntwork of the OAuth provider configuration work for you.
These classes are "missing" in Xamarin apparently and there is an extended discussion about best practice in this thread on the Xamarin forums.
This thread can be summarised as:
IMHO the XML config file would be my preference with a reader class that is a Singleton implementation, much like the ConfigurationManager
is. Why? Because XML config files are an established pattern in .NET and anyone reading your code will understand how/why you're working with the class that way...
If you put your Singleton in a commonly accessible assembly and make sure that it loads the config on construction, it should be available in multiple places without needing to worry about duplicating the code or reinstantiating after the first construction for the lifetime of the application and if you need it, you simply add a dependency to that assembly and use the instance.
Upvotes: 1
Reputation: 157048
You have to create classes, at least to hold the data, possibly even to do some custom work for the specific provider.
Something like this:
public class OAuthInfo
{
public string AppId {get;set;}
public string Scope {get;set;}
public string AppName {get;set;}
public string AuthUri {get;set;}
public string ReturnUri {get;set;}
}
And instantiate an instance:
var facebookData = new OAuthInfo()
{
AppId = "xxxxxx",
Scope = "",
AppName = "xxxx",
AuthUri = "https://m.facebook.com/dialog/oauth/",
ReturnUri = "https://www.facebook.com/connect/login_success.html"
};
You can pass that along where you need it.
Upvotes: 3