{"Name":"Xamarin.Auth","Id":395,"Alias":"xamarin.auth","Description":"Xamarin.Auth helps you authenticate users via standard authentication mechanisms (e.g. OAuth 1.0 and 2.0), and store user credentials. It\u0027s also straightforward to add support for non-standard authentication schemes. The library is cross-platform, so once you learn it on iOS, you\u0027re all set on Android.\n\n```csharp\nusing Xamarin.Auth;\n\nvar auth = new OAuth2Authenticator (\n\tclientId: \"App ID from https://developers.facebook.com/apps\",\n\tscope: \"\",\n\tauthorizeUrl: new Uri (\"https://m.facebook.com/dialog/oauth/\"),\n\tredirectUrl: new Uri (\"http://www.facebook.com/connect/login_success.html\"));\n\nauth.Completed += (sender, eventArgs) =\u003e {\n\tDismissViewController (true, null);\n\tif (eventArgs.IsAuthenticated) {\n\t\t// Use eventArgs.Account to do wonderful things\n\t}\n}\n\nPresentViewController (auth.GetUI (), true, null);\n```\n\nIt\u0027s that easy to authenticate users!\n\n*Some screenshots assembled with [PlaceIt](http://placeit.breezi.com/).*\n","Version":"1.2.2","Summary":"A cross-platform API for authenticating users and storing their accounts.","QuickStart":"## 1. Create and configure an authenticator\n\nLet\u0027s authenticate a user to access Facebook:\n\n```csharp\nusing Xamarin.Auth;\n...\nvar auth = new OAuth2Authenticator (\n\tclientId: \"App ID from https://developers.facebook.com/apps\",\n\tscope: \"\",\n\tauthorizeUrl: new Uri (\"https://m.facebook.com/dialog/oauth/\"),\n\tredirectUrl: new Uri (\"http://www.facebook.com/connect/login_success.html\"));\n```\n\nFacebook uses OAuth 2.0 authentication, so we create an `OAuth2Authenticator`. Authenticators are responsible for managing the user interface and communicating with authentication services.\n\nAuthenticators take a variety of parameters; in this case, the application\u0027s ID, its authorization scope, and Facebook\u0027s various service locations are required.\n\n\n\n\n## 2. Authenticate the user\n\nWhile authenticators manage their own UI, it\u0027s up to you to initially present the authenticator\u0027s UI on the screen. This lets you control how the authentication UI is displayed–modally, in navigation controllers, in popovers, etc.\n\nBefore we present the UI, we need to start listening to the `Completed` event which fires when the user successfully authenticates or cancels. You can find out if the authentication succeeded by testing the `IsAuthenticated` property of `eventArgs`:\n\n```csharp\nauth.Completed += (sender, eventArgs) =\u003e {\n\t// We presented the UI, so it\u0027s up to us to dimiss it on iOS.\n\tDismissViewController (true, null);\n\n\tif (eventArgs.IsAuthenticated) {\n\t\t// Use eventArgs.Account to do wonderful things\n\t} else {\n\t\t// The user cancelled\n\t}\n};\n```\n\nAll the information gathered from a successful authentication is available in `eventArgs.Account`.\n\nNow we\u0027re ready to present the login UI from `ViewDidAppear` on iOS:\n\n```csharp\nPresentViewController (auth.GetUI (), true, null);\n```\n\nThe `GetUI` method returns `UINavigationControllers` on iOS, and `Intents` on Android. On Android, we would write the following code to present the UI from `OnCreate`:\n\n```csharp\nStartActivity (auth.GetUI (this));\n```\n\n\n\n## 3. Making requests\n\nSince Facebook is an OAuth2 service, we\u0027ll make requests with `OAuth2Request` providing the account we retrieved from the `Completed` event. Assuming we\u0027re authenticated, we\u0027ll grab the user\u0027s info to demonstrate:\n\n```csharp\nvar request = new OAuth2Request (\"GET\", new Uri (\"https://graph.facebook.com/me\"), null, eventArgs.Account);\nrequest.GetResponseAsync().ContinueWith (t =\u003e {\n\tif (t.IsFaulted)\n\t\tConsole.WriteLine (\"Error: \" + t.Exception.InnerException.Message);\n\telse {\n\t\tstring json = t.Result.GetResponseText();\n\t\tConsole.WriteLine (json);\n\t}\n});\n```\n\n\n## 4. Store the account\n\nXamarin.Auth securely stores `Account` objects so that you don\u0027t always have to reauthenticate the user. The `AccountStore` class is responsible for storing `Account` information, backed by the [Keychain](https://developer.apple.com/library/ios/#documentation/security/Reference/keychainservices/Reference/reference.html) on iOS and a [KeyStore](http://developer.android.com/reference/java/security/KeyStore.html) on Android:\n\n```csharp\n// On iOS:\nAccountStore.Create ().Save (eventArgs.Account, \"Facebook\");\n\n// On Android:\nAccountStore.Create (this).Save (eventArgs.Account, \"Facebook\");\n```\n\nSaved Accounts are uniquely identified using a key composed of the account\u0027s `Username` property and a \"Service ID\". The \"Service ID\" is any string that is used when fetching accounts from the store.\n\nIf an `Account` was previously saved, calling `Save` again will overwrite it. This is convenient for services that expire the credentials stored in the account object.\n\n\n\n\n## 5. Retrieve stored accounts\n\nYou can fetch all `Account` objects stored for a given service:\n\n```csharp\n// On iOS:\nIEnumerable\u003cAccount\u003e accounts = AccountStore.Create ().FindAccountsForService (\"Facebook\");\n\n// On Android:\nIEnumerable\u003cAccount\u003e accounts = AccountStore.Create (this).FindAccountsForService (\"Facebook\");\n```\n\nIt\u0027s that easy.\n\n\n\n\n## 6. Make your own authenticator\n\nXamarin.Auth includes OAuth 1.0 and OAuth 2.0 authenticators, providing support for thousands of popular services. For services that use traditional username/password authentication, you can roll your own authenticator by deriving from `FormAuthenticator`.\n\nIf you want to authenticate against an ostensibly unsupported service, fear not – Xamarin.Auth is extensible! It\u0027s very easy to create your own authenticators – just derive from any of the existing authenticators and start overriding methods.\n\n\n","Hash":"1ff1198362bc194410ab5308d4d5ded4","TargetPlatforms":["ios","android"],"TrialHash":null}