🔐auth0-net-ios
- プラグイン
- auth0
- ライセンス
- Apache-2.0
- ソース
- GitHub で見る ↗
説明
次のような場合に使用: .NET iOSアプリケーションにAuth0ログインまたはトークン管理を追加する場合。 Auth0.OidcClient.iOS を統合します — ユーザーが「.NET iOSアプリにログインを追加したい」と言った場合や、Xamarin iOSに言及した場合にも使用してください。
原文を表示
Use when adding Auth0 login or token management to a .NET iOS application. Integrates Auth0.OidcClient.iOS — use even if the user says "add login to my .NET iOS app" or references Xamarin iOS.
ユースケース
- ✓.NET iOSアプリにAuth0ログインを追加するとき
- ✓iOSアプリのトークン管理を実装するとき
- ✓Xamarin iOSでAuth0認証を統合するとき
本文(日本語訳)
auth0-net-ios インテグレーション
.NET iOS アプリケーションに Auth0 認証を追加します。
このスキルは Auth0.OidcClient.iOS NuGet パッケージを統合し、ASWebAuthenticationSession を使用して PKCE による安全な OIDC ベースのログイン・ログアウトフローを実現します。
Agent への指示: SDK セットアップ手順を提供する前に、以下のコマンドを実行して最新リリースバージョンを取得してください:
gh api repos/auth0/auth0-oidc-client-net/releases --jq '[.[] | select(.tag_name | startswith("ios-")) | .tag_name | ltrimstr("ios-")] | .[0]'以下に記載されているハードコードされたバージョンの代わりに、返されたバージョンをすべての依存関係の記述に使用してください。
前提条件
- .NET 8.0 SDK 以降(.NET 9 にも対応 — ターゲットフレームワークに
net9.0-iosを使用) - JetBrains Rider または VS Code と C# Dev Kit(iOS ワークロード込み)
- macOS と Xcode 15 以降
- iOS 14.0 以降のデプロイメントターゲット
- Native アプリケーションが設定済みの Auth0 アカウント
- Node.js 20 以降(Auth0 CLI による自動セットアップ用)
このスキルを使用しない場合
| ユースケース | 推奨スキル |
|---|---|
| .NET MAUI クロスプラットフォームアプリ(iOS + Android + Windows) | auth0-maui |
| .NET Android 専用アプリ | auth0-net-android |
| iOS 専用 Swift アプリ | auth0-swift |
| ASP.NET Core サーバーサイド Web アプリ | auth0-aspnetcore-authentication |
| ASP.NET Core Web API(JWT 検証) | auth0-aspnetcore-api |
| React Native モバイルアプリ | auth0-react-native |
クイックスタート手順
Agent への指示: 開始前に、ユーザーのプロジェクトを以下の観点で確認してください:
.csprojファイルのTargetFrameworkから .NET バージョンを特定する- 既存の認証実装を確認する — 既存のログイン/ログアウトハンドラーを検索し、存在する場合はそれに組み込む(ログインボタンなど既存の UI 要素を再利用し、重複を作らない)
Info.plistまたは.csprojからプロジェクトの Bundle Identifier を確認する- 設定の重複を避けるため、既存の
Auth0ClientまたはAuth0ClientOptionsの使用箇所を確認する
- SDK のインストール:
dotnet add package Auth0.OidcClient.iOS - Auth0 の設定: 自動または手動設定については セットアップガイド を参照してください。
- 認証の統合:
Auth0Clientのインスタンス化、Info.plistへの URL スキームの登録、ログイン/ログアウトと UI アクションの紐付けを行います。 - コールバックの処理:
AppDelegateにOpenUrlを実装し、ActivityMediator.Instance.Send(url.AbsoluteString)を呼び出します。 - ビルドと確認:
dotnet build
Agent への指示: Auth0Client の設定を記述する際:
- iOS SDK は Activity コンテキストの渡しを必要としません —
new Auth0Client(options)のみで構いません。Scope = "openid profile email offline_access"を必ず設定してください —offline_accessスコープはリフレッシュトークンの受け取りに必要であり、ユーザーへの再認証プロンプトなしにサイレントなトークン更新を可能にします。- コールバック URL は Bundle Identifier から自動的に導出されます:
{BundleId}://{domain}/ios/{BundleId}/callback- Bundle Identifier は
Info.plistに URL スキームとして登録する必要があります。AppDelegateはOpenUrlを処理し、ActivityMediator.Instance.Send(url.AbsoluteString)を呼び出す必要があります。- トークンを安全に保存してください: ログイン成功後、
AccessTokenとRefreshTokenを iOS Keychain(Securityフレームワーク、またはKeychainAccessなどのラッパー経由)を使用して永続化してください。トークンをUserDefaultsやインメモリ変数のみに保存しないでください。設定とコードを記述した後、ビルドが成功することを確認してください:
dotnet buildビルドが失敗した場合は問題の修正を試みてください。5〜6 回の試行後も失敗する場合は、ユーザーに助けを求めてください。
WebAuth — 認証の仕組み
SDK は ASWebAuthenticationSession(セキュアなシステムブラウザ)を使用します。LoginAsync() が呼び出されると:
- SDK が PKCE パラメーター(コードベリファイアとチャレンジ)を含む
/authorizeURL を構築します - ASWebAuthenticationSession が開き、Auth0 のログインページが表示されます
- ユーザーが認証を行います(ログインフォーム、ソーシャル連携、MFA など)
- Auth0 がネイティブコールバック URL にリダイレクトします:
{BundleId}://{domain}/ios/{BundleId}/callback - iOS が URL スキームをインターセプトし、
AppDelegate.OpenUrlに届けます ActivityMediator.Instance.Send(url.AbsoluteString)がトークン交換を完了します- SDK がアクセストークン、ID トークン、リフレッシュトークン、およびユーザークレームを含む
LoginResultを返します
これはネイティブモバイルアプリケーションに推奨される、PKCE を用いた標準の OAuth 2.0 Authorization Code フローです。
コールバック URL の設定
.NET iOS のネイティブコールバック URL は、Bundle Identifier をスキームとして使用します。フォーマットは以下の通りです:
YOUR_BUNDLE_IDENTIFIER://YOUR_AUTH0_DOMAIN/ios/YOUR_BUNDLE_IDENTIFIER/callback
YOUR_BUNDLE_IDENTIFIER はアプリケーションの Bundle Identifier(例: com.mycompany.myapplication)です。
例: com.mycompany.myapp://tenant.us.auth0.com/ios/com.mycompany.myapp/callback
注意: Auth0 の一部のネイティブ SDK では、コールバック URL のフォーマットとして
https://{domain}/ios/{bundleId}/callbackや{bundleId}.auth0://{domain}/ios/{bundleId}/callbackを使用します。.NET iOS SDK では Bundle Identifier を URL スキームとして直接使用します。
コールバック URL は必ず小文字にしてください。
この URL は以下の両方に登録する必要があります:
- Auth0 ダッシュボードの Allowed Callback URLs および Allowed Logout URLs
Info.plistのCFBundleURLSchemesへの URL スキームとして
完了の条件
- [ ]
Auth0.OidcClient.iOSパッケージのインストール(最新の安定版) - [ ] Domain、ClientId、および
Scope = "openid profile email offline_access"を使用したAuth0Clientの設定 - [ ] Bundle Identifier と一致する URL スキームの
Info.plistへの登録 - [ ]
ActivityMediator.Instance.Send(url.AbsoluteString)を使用したAppDelegate.OpenUrlの実装 - [ ] Auth0 ダッシュボードの Allowed Callback URLs および Allowed Logout URLs へのコールバック URL の追加
- [ ] iOS Keychain(
SecKeyChain.Addを使用したSecurityフレームワーク)によるトークンの安全な保存 - [ ] ログイン/ログアウトフローの動作確認
- [ ] エラーなしでのビルド成功
詳細ドキュメント
- セットアップガイド — Auth0 テナントの設定、SDK のインストール、Info.plist の URL スキーム設定
- インテグレーションパターン — ログイン/ログアウトフロー、トークンアクセス、ユーザープロファイル、エラーハンドリング
- API リファレンスとテスト —
Auth0ClientOptionsの完全なリファレンス、クレーム、テストチェックリスト、トラブルシューティング
よくある間違い
| 間違い | 修正方法 |
|---|---|
| Auth0 ダッシュボードでアプリタイプが Native に設定されていない | ダッシュボード設定でアプリケーションタイプを "Native" に変更する |
| Auth0 ダッシュボードにコールバック URL が未登録 | Allowed Callback URLs と Allowed Logout URLs の両方に yourbundleid://{domain}/ios/yourbundleid/callback を追加する |
| Info.plist に URL スキームが未設定 | Bundle Identifier と一致する CFBundleURLSchemes エントリを追加する |
AppDelegate で OpenUrl を処理していない |
OpenUrl をオーバーライドし、ActivityMediator.Instance.Send(url.AbsoluteString) を呼び出す |
Domain に https:// プレフィックスを使用している |
Domain はホスト名のみにする(例: https://tenant.auth0.com ではなく tenant.auth0.com) |
| Auth0Client に Activity コンテキストを渡している(Android のパターン) | iOS ではコンテキスト不要 — new Auth0Client(options) のみで構いません |
| URL スキームが Bundle Identifier と一致していない | CFBundleURLSchemes の値は Bundle Identifier と完全に一致させる必要があります |
トークンを UserDefaults またはメモリのみに保存している |
トークンの永続化には Security フレームワーク(SecKeyChain.Add)経由の iOS Keychain を使用する |
テストに関する注意事項
Agent への指示: シミュレーターに加えて実機でのテストも行うようユーザーに伝えてください。WebAuth/ASWebAuthenticationSession の動作は実機とシミュレーターで異なる場合があります。リリース前に実機でログイン → コールバック → トークン取得の完全なフローをテストしてください。
実機テスト:
- ログインフロー: ASWebAuthenticationSession が開く → 認証 → アプリに戻る
- コールバック: AppDelegate の
OpenUrlが正しい URL で呼び出される - ログアウトフロー: ブラウザが開く → セッションがクリアされる → アプリに戻る
- キャンセル: ユーザーがシートを閉じる → アプリが
UserCancelを適切に処理する
関連スキル
- auth0-maui — .NET MAUI クロスプラットフォームアプリ(iOS + Android + Windows)
- auth0-net-android — .NET Android 専用アプリ
- auth0-swift — iOS 専用 Swift アプリ
- auth0-aspnetcore-authentication — ASP.NET Core サーバーサイド Web アプリ
- auth0-aspnetcore-api — JWT 検証を使用した ASP.NET Core Web API
クイックリファレンス
using Auth0.OidcClient;
var client = new Auth0Client(new Auth0ClientOptions
{
Domain = "YOUR_AUTH0_DOMAIN",
ClientId = "YOUR_AUTH0_CLIENT_ID",
Scope = "openid profile email offline_access"
});
var loginResult = await client.LoginAsync();
BrowserResultType browserResult = await client.LogoutAsync();
Agent への指示: リフレッシュトークンを有効にするため、スコープには必ず
offline_accessを含めてください。これにより、ユーザーへの再認証を強制することなく、アクセストークンをサイレントに更新できます。Agent への指示: ログイン後は、
Securityフレームワーク(SecAccessible.WhenUnlockedThisDeviceOnlyを指定したSecKeyChain.Add)を使用して、必ずトークンを iOS Keychain に永続化してください。トークンをUserDefaultsに保存したり、メモリのみに保持したりしないでください。ログアウト時にはトークンをクリアしてください。完全なSecureTokenStorageヘルパークラスについては インテグレーションパターン を参照してください。
必須プラットフォーム設定
コールバックを機能させるために以下の 2 つの設定が必要です — 完全なコードは セットアップガイド を参照してください:
- Info.plist: Bundle Identifier と一致する
CFBundleURLSchemesエントリを追加する - AppDelegate:
OpenUrlをオーバーライドし、ActivityMediator.Instance.Send(url.AbsoluteString)を呼び出す
追加パラメーターを使用したログイン、エラーハンドリング、トークンのリフレッシュ、ユーザークレームのアクセス、および完全な ViewController の例については、[インテグレーションパターン
原文(English)を表示
auth0-net-ios Integration
Add Auth0 authentication to .NET iOS applications. This skill integrates the Auth0.OidcClient.iOS NuGet package which uses ASWebAuthenticationSession for secure OIDC-based login and logout flows with PKCE.
Agent instruction: Before providing SDK setup instructions, fetch the latest release version by running:
gh api repos/auth0/auth0-oidc-client-net/releases --jq '[.[] | select(.tag_name | startswith("ios-")) | .tag_name | ltrimstr("ios-")] | .[0]'Use the returned version in all dependency lines instead of any hardcoded version below.
Prerequisites
- .NET 8.0 SDK or later (.NET 9 also supported — use
net9.0-iostarget framework) - JetBrains Rider or VS Code with C# Dev Kit (with iOS workload)
- macOS with Xcode 15+
- iOS 14.0+ deployment target
- Auth0 account with a Native application configured
- Node.js 20+ (for Auth0 CLI automated setup)
When NOT to Use
| Use Case | Recommended Skill |
|---|---|
| .NET MAUI cross-platform app (iOS + Android + Windows) | auth0-maui |
| .NET Android-only app | auth0-net-android |
| iOS-only Swift app | auth0-swift |
| ASP.NET Core server-side web app | auth0-aspnetcore-authentication |
| ASP.NET Core Web API (JWT validation) | auth0-aspnetcore-api |
| React Native mobile app | auth0-react-native |
Quick Start Workflow
Agent instruction: Before starting, examine the user's project:
- Identify the .NET version from the
.csprojfile (TargetFramework)- Check for existing authentication implementations — search for existing login/logout handlers and hook into them if found (reuse existing UI elements like login buttons rather than creating duplicates)
- Note the project's Bundle Identifier from
Info.plistor.csproj- Look for existing
Auth0ClientorAuth0ClientOptionsusage to avoid duplicate configuration
- Install SDK:
dotnet add package Auth0.OidcClient.iOS - Configure Auth0: See Setup Guide for automatic or manual configuration.
- Integrate authentication: Add
Auth0Clientinstantiation, register the URL scheme inInfo.plist, and wire login/logout to UI actions. - Handle callback: Implement
OpenUrlinAppDelegateand callActivityMediator.Instance.Send(url.AbsoluteString). - Build and verify:
dotnet build
Agent instruction: When writing the Auth0Client configuration:
- The iOS SDK does NOT require passing an Activity context — just
new Auth0Client(options).- Always set
Scope = "openid profile email offline_access"— theoffline_accessscope is required to receive refresh tokens, enabling silent token renewal without re-prompting the user.- The callback URL is automatically derived from the Bundle Identifier:
{BundleId}://{domain}/ios/{BundleId}/callback.- The Bundle Identifier must be registered as a URL scheme in
Info.plist.- The
AppDelegatemust handleOpenUrland callActivityMediator.Instance.Send(url.AbsoluteString).- Store tokens securely: After successful login, persist
AccessTokenandRefreshTokenusing iOS Keychain (viaSecurityframework or a wrapper likeKeychainAccess). Never store tokens inUserDefaultsor in-memory variables only.After writing configuration and code, verify the build succeeds:
dotnet buildIf the build fails, attempt to fix the issue. After 5-6 failed attempts, ask the user for help.
WebAuth — How Authentication Works
The SDK uses ASWebAuthenticationSession (the secure system browser). When LoginAsync() is called:
- SDK constructs the
/authorizeURL with PKCE parameters (code verifier + challenge) - ASWebAuthenticationSession opens showing the Auth0 login page
- User authenticates (login form, social connections, MFA, etc.)
- Auth0 redirects to the native callback URL:
{BundleId}://{domain}/ios/{BundleId}/callback - iOS intercepts the URL scheme and delivers it to
AppDelegate.OpenUrl ActivityMediator.Instance.Send(url.AbsoluteString)completes the token exchange- SDK returns
LoginResultwith access token, ID token, refresh token, and user claims
This is the standard OAuth 2.0 Authorization Code flow with PKCE, recommended for native mobile applications.
Callback URL Configuration
The native callback URL for .NET iOS uses the Bundle Identifier as the scheme. The format is:
YOUR_BUNDLE_IDENTIFIER://YOUR_AUTH0_DOMAIN/ios/YOUR_BUNDLE_IDENTIFIER/callback
Where YOUR_BUNDLE_IDENTIFIER is the Bundle Identifier for your application, such as com.mycompany.myapplication. For example: com.mycompany.myapp://tenant.us.auth0.com/ios/com.mycompany.myapp/callback.
Note: Some Auth0 native SDKs use
https://{domain}/ios/{bundleId}/callbackor{bundleId}.auth0://{domain}/ios/{bundleId}/callbackas the callback URL format. The .NET iOS SDK uses the Bundle Identifier directly as the URL scheme.
Ensure that the Callback URL is in lowercase.
This URL must be:
- Registered in Auth0 Dashboard under Allowed Callback URLs and Allowed Logout URLs
- Registered as a URL scheme in
Info.plistunderCFBundleURLSchemes
Done When
- [ ]
Auth0.OidcClient.iOSpackage installed (latest stable version) - [ ]
Auth0Clientconfigured with Domain, ClientId, andScope = "openid profile email offline_access" - [ ] URL scheme registered in
Info.plistmatching the Bundle Identifier - [ ]
AppDelegate.OpenUrlimplemented withActivityMediator.Instance.Send(url.AbsoluteString) - [ ] Callback URL added to Auth0 Dashboard Allowed Callback URLs and Allowed Logout URLs
- [ ] Tokens stored securely using iOS Keychain (
Securityframework withSecKeyChain.Add) - [ ] Login/logout flow working
- [ ] Build succeeds with no errors
Detailed Documentation
- Setup Guide — Auth0 tenant configuration, SDK installation, Info.plist URL scheme setup
- Integration Patterns — Login/logout flows, token access, user profile, error handling
- API Reference & Testing — Full
Auth0ClientOptionsreference, claims, testing checklist, troubleshooting
Common Mistakes
| Mistake | Fix |
|---|---|
| App type not set to Native in Auth0 Dashboard | Change application type to "Native" in Dashboard settings |
| Missing callback URL in Auth0 Dashboard | Add yourbundleid://{domain}/ios/yourbundleid/callback to Allowed Callback URLs AND Allowed Logout URLs |
| Missing URL scheme in Info.plist | Add CFBundleURLSchemes entry matching your Bundle Identifier |
Not handling OpenUrl in AppDelegate |
Override OpenUrl and call ActivityMediator.Instance.Send(url.AbsoluteString) |
Using https:// prefix in Domain |
Domain should be hostname only (e.g., tenant.auth0.com, not https://tenant.auth0.com) |
| Passing Activity context to Auth0Client (Android pattern) | iOS does not need context — just new Auth0Client(options) |
| URL scheme not matching Bundle Identifier | The CFBundleURLSchemes value must exactly match the Bundle Identifier |
Storing tokens in UserDefaults or only in memory |
Use iOS Keychain via the Security framework (SecKeyChain.Add) for token persistence |
Testing Notes
Agent instruction: Remind the user to test on a physical device in addition to simulators. Some WebAuth/ASWebAuthenticationSession behaviors may differ on physical devices vs. simulators. Test the full login → callback → token flow on real hardware before shipping.
Physical Device Testing:
- Login flow: ASWebAuthenticationSession opens → authenticate → returns to app
- Callback:
OpenUrlfires in AppDelegate with correct URL - Logout flow: Browser opens → session cleared → returns to app
- Cancel: User dismisses sheet → app handles
UserCancelgracefully
Related Skills
- auth0-maui — .NET MAUI cross-platform apps (iOS + Android + Windows)
- auth0-net-android — .NET Android-only apps
- auth0-swift — iOS-only Swift apps
- auth0-aspnetcore-authentication — ASP.NET Core server-side web apps
- auth0-aspnetcore-api — ASP.NET Core Web API with JWT validation
Quick Reference
using Auth0.OidcClient;
var client = new Auth0Client(new Auth0ClientOptions
{
Domain = "YOUR_AUTH0_DOMAIN",
ClientId = "YOUR_AUTH0_CLIENT_ID",
Scope = "openid profile email offline_access"
});
var loginResult = await client.LoginAsync();
BrowserResultType browserResult = await client.LogoutAsync();
Agent instruction: Always include
offline_accessin the scope to enable refresh tokens. This allows the app to silently refresh access tokens without forcing the user to re-authenticate.Agent instruction: After login, always persist tokens to the iOS Keychain using the
Securityframework (SecKeyChain.AddwithSecAccessible.WhenUnlockedThisDeviceOnly). Never store tokens inUserDefaultsor leave them only in memory. Clear tokens on logout. See Integration Patterns for the fullSecureTokenStoragehelper class.
Required Platform Configuration
These two pieces are required for the callback to work — see Setup Guide for full code:
- Info.plist: Add
CFBundleURLSchemesentry matching the Bundle Identifier - AppDelegate: Override
OpenUrland callActivityMediator.Instance.Send(url.AbsoluteString)
For login with extra parameters, error handling, token refresh, user claims access, and complete ViewController examples, see Integration Patterns.
References
原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。