フッターコンテンツにスキップ
.NETヘルプ

Dotnetopenauth .NET Core(開発者向けの動作方法)

DotNetOpenAuth .NET Coreは、オリジナルのDotNetOpenAuthライブラリを.NET Core向けに適応したバージョンであり、強力な公開APIを提供します。 このライブラリは、OAuth2とOpenIDを使用して .NET アプリケーションに認証機能を追加するのに役立ちます。 IronPDFは、.NETでPDFファイルを作成、読み取り、および編集するためのライブラリです。 これは、.NETアプリケーションから直接レポートや請求書などのドキュメントを生成するのに便利です。

DotNetOpenAuth .NET CoreとIronPDFは、ウェブおよびデスクトップアプリケーションなどのさまざまな種類のプロジェクトで使用して、共有コードを活用し、新機能を実装できます。 これらは、ソフトウェアで認証とPDFドキュメント管理を扱うことを考えている開発者にとって不可欠です。

DotNetOpenAuth .NET Coreの開始

DotNetOpenAuth .NET Core の .NET プロジェクトへの設定

Microsoft技術にサポートされているDotNetOpenAuth .NET Coreを .NET プロジェクトで使用し始めるには、次の手順に従ってください:

  1. Visual Studioでプロジェクトを開きます。
  2. ソリューションエクスプローラに移動します。
  3. プロジェクト名を右クリックします。
  4. Manage NuGet Packagesを選択します。
  5. NuGetパッケージ マネージャーで、DotNetOpenAuth.NetCore およびその他のNuGetパッケージを検索します。
  6. Install をクリックしてプロジェクトに追加します。

これにより、DotNetOpenAuth .NET Coreライブラリがプロジェクトに追加され、認証機能を統合するサポートが提供されます。

DotNetOpenAuth .NET Coreを使用した基本コード例

ここに、DotNetOpenAuth .NET Coreを使用してアプリケーションでOAuth2認証をセットアップする方法を示すシンプルな例があります:

using DotNetOpenAuth.OAuth2;

// Initialize the OAuth2 client with the authorization server details
var client = new WebServerClient(new AuthorizationServerDescription
{
    TokenEndpoint = new Uri("https://your-auth-server.com/token"),
    AuthorizationEndpoint = new Uri("https://your-auth-server.com/authorize")
}, "your-client-id", "your-client-secret");

// Start the authentication process and get the authorization state
IAuthorizationState state = client.ProcessUserAuthorization();
if (state != null && state.IsAuthorized)
{
    // Authorized successfully, now you can access protected resources
}
using DotNetOpenAuth.OAuth2;

// Initialize the OAuth2 client with the authorization server details
var client = new WebServerClient(new AuthorizationServerDescription
{
    TokenEndpoint = new Uri("https://your-auth-server.com/token"),
    AuthorizationEndpoint = new Uri("https://your-auth-server.com/authorize")
}, "your-client-id", "your-client-secret");

// Start the authentication process and get the authorization state
IAuthorizationState state = client.ProcessUserAuthorization();
if (state != null && state.IsAuthorized)
{
    // Authorized successfully, now you can access protected resources
}
Imports DotNetOpenAuth.OAuth2

' Initialize the OAuth2 client with the authorization server details
Private client = New WebServerClient(New AuthorizationServerDescription With {
	.TokenEndpoint = New Uri("https://your-auth-server.com/token"),
	.AuthorizationEndpoint = New Uri("https://your-auth-server.com/authorize")
}, "your-client-id", "your-client-secret")

' Start the authentication process and get the authorization state
Private state As IAuthorizationState = client.ProcessUserAuthorization()
If state IsNot Nothing AndAlso state.IsAuthorized Then
	' Authorized successfully, now you can access protected resources
End If
$vbLabelText   $csharpLabel

このコードスニペットは、DotNetOpenAuth .NET Coreを使用してOAuth2クライアントをセットアップし、認証サーバーに接続してユーザー認証を処理します。

DotNetOpenAuth .NET Coreの機能の実装

OpenID Connectの統合

DotNetOpenAuth .NET Coreを使用してOpenID Connectを統合するには、次の基本的なアプローチを使用できます:

using DotNetOpenAuth.OAuth2;

// Configure the OpenID Connect client with authority details
var openIdClient = new WebServerClient(new AuthorizationServerDescription
{
    TokenEndpoint = new Uri("https://your-openid-provider.com/token"),
    AuthorizationEndpoint = new Uri("https://your-openid-provider.com/authorize")
}, "your-client-id");

// Redirect user for authentication
Uri authUri = openIdClient.GetAuthorizationRequestUri("openid email profile");
Response.Redirect(authUri.AbsoluteUri);
using DotNetOpenAuth.OAuth2;

// Configure the OpenID Connect client with authority details
var openIdClient = new WebServerClient(new AuthorizationServerDescription
{
    TokenEndpoint = new Uri("https://your-openid-provider.com/token"),
    AuthorizationEndpoint = new Uri("https://your-openid-provider.com/authorize")
}, "your-client-id");

// Redirect user for authentication
Uri authUri = openIdClient.GetAuthorizationRequestUri("openid email profile");
Response.Redirect(authUri.AbsoluteUri);
Imports DotNetOpenAuth.OAuth2

' Configure the OpenID Connect client with authority details
Private openIdClient = New WebServerClient(New AuthorizationServerDescription With {
	.TokenEndpoint = New Uri("https://your-openid-provider.com/token"),
	.AuthorizationEndpoint = New Uri("https://your-openid-provider.com/authorize")
}, "your-client-id")

' Redirect user for authentication
Private authUri As Uri = openIdClient.GetAuthorizationRequestUri("openid email profile")
Response.Redirect(authUri.AbsoluteUri)
$vbLabelText   $csharpLabel

このコードはOpenID Connectクライアントを設定し、ユーザーをOpenIDプロバイダーの認証ページにリダイレクトします。

アクセストークンの処理

DotNetOpenAuth .NET Coreでのアクセストークンの処理方法はこちらです:

// After the user is authenticated, process the authorization response to retrieve the token
IAuthorizationState authState = openIdClient.ProcessUserAuthorization();
if (authState != null && authState.IsAuthorized)
{
    // Access token is available, and you can use it to make authenticated requests
    string accessToken = authState.AccessToken;
}
// After the user is authenticated, process the authorization response to retrieve the token
IAuthorizationState authState = openIdClient.ProcessUserAuthorization();
if (authState != null && authState.IsAuthorized)
{
    // Access token is available, and you can use it to make authenticated requests
    string accessToken = authState.AccessToken;
}
' After the user is authenticated, process the authorization response to retrieve the token
Dim authState As IAuthorizationState = openIdClient.ProcessUserAuthorization()
If authState IsNot Nothing AndAlso authState.IsAuthorized Then
	' Access token is available, and you can use it to make authenticated requests
	Dim accessToken As String = authState.AccessToken
End If
$vbLabelText   $csharpLabel

このスニペットはユーザー認証レスポンスを処理し、アクセストークンを取得して使用します。

トークンの更新

トークンが期限切れになったときに更新するには、次のコードを使用します:

// Check if the access token is expired and refresh it
if (authState.AccessTokenExpirationUtc <= DateTime.UtcNow)
{
    if (openIdClient.RefreshAuthorization(authState))
    {
        // Token refreshed successfully
    }
}
// Check if the access token is expired and refresh it
if (authState.AccessTokenExpirationUtc <= DateTime.UtcNow)
{
    if (openIdClient.RefreshAuthorization(authState))
    {
        // Token refreshed successfully
    }
}
' Check if the access token is expired and refresh it
If authState.AccessTokenExpirationUtc <= DateTime.UtcNow Then
	If openIdClient.RefreshAuthorization(authState) Then
		' Token refreshed successfully
	End If
End If
$vbLabelText   $csharpLabel

このコードは現在のトークンが期限切れかどうかをチェックし、更新を試みます。

トークンの取り消し

トークンを取り消す必要がある場合は、以下のように実装します:

// Revoke the access token
bool success = openIdClient.RevokeAuthorization(authState);
if (success)
{
    // Token revoked successfully
}
// Revoke the access token
bool success = openIdClient.RevokeAuthorization(authState);
if (success)
{
    // Token revoked successfully
}
' Revoke the access token
Dim success As Boolean = openIdClient.RevokeAuthorization(authState)
If success Then
	' Token revoked successfully
End If
$vbLabelText   $csharpLabel

このスニペットは認証を取り消し、効果的にアクセストークンを無効化します。

トークンリクエストのカスタマイズ

特定のニーズに応じてトークンリクエストをカスタマイズするには、追加のパラメーターを加えることができます:

// Customize the token request with additional parameters
var additionalParams = new Dictionary<string, string>
{
    {"custom_parameter", "value"}
};
IAuthorizationState customizedState = openIdClient.ProcessUserAuthorization(additionalParams);
if (customizedState != null && customizedState.IsAuthorized)
{
    // Token request customized and processed successfully
}
// Customize the token request with additional parameters
var additionalParams = new Dictionary<string, string>
{
    {"custom_parameter", "value"}
};
IAuthorizationState customizedState = openIdClient.ProcessUserAuthorization(additionalParams);
if (customizedState != null && customizedState.IsAuthorized)
{
    // Token request customized and processed successfully
}
' Customize the token request with additional parameters
Dim additionalParams = New Dictionary(Of String, String) From {
	{"custom_parameter", "value"}
}
Dim customizedState As IAuthorizationState = openIdClient.ProcessUserAuthorization(additionalParams)
If customizedState IsNot Nothing AndAlso customizedState.IsAuthorized Then
	' Token request customized and processed successfully
End If
$vbLabelText   $csharpLabel

このコードはトークンリクエストにカスタムパラメーターを追加し、認可サーバーの特定の要件に対応するのに役立ちます。

IronPDFを使ったDotNetOpenAuth .NET Core

IronPDFは、.NET環境でPDFファイルを作成、読み取り、および操作することを開発者に許可する包括的なライブラリです。 HTMLから、または直接URLからPDFを生成するのに特に便利で、レポート作成や請求書の生成、またはウェブページを静的フォーマットで保存するのに最適です。 DotNetOpenAuth .NET Coreと統合すると、これらの機能が安全で、認証されたユーザーのみがアクセス可能であることが保証されます。

DotNetOpenAuth .NET CoreとIronPDFを統合した使用例

認証されたユーザーが個別のレポートを生成してダウンロードする必要があるウェブアプリケーションで、DotNetOpenAuth .NET CoreとIronPDFを統合した実用的な使用例があります。 たとえば、ユーザーがアプリケーションにログインして、PDF形式の財務レポートにアクセスする状況を考えてみてください。 DotNetOpenAuthは、ユーザーが適切に認証されており、ドキュメントへのアクセスが許可されていることを保証し、IronPDFはこれらの個別のPDFの作成と配信を処理します。

ユースケースのコード例

これを実装する方法を示す完全なコード例を見てみましょう。 私たちは、ユーザーを認証し、その後、IronPDFを使用してPDFレポートを生成する簡単な.NET CoreウェブAPIを作成します:

using IronPdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

[Route("api/[controller]")]
[ApiController]
public class ReportController : ControllerBase
{
    [Authorize]
    [HttpGet("download-pdf")]
    public IActionResult DownloadPdfReport()
    {
        // Authentication is handled by DotNetOpenAuth .NET Core
        var currentUser = HttpContext.User.Identity.Name;

        // Generate PDF content using IronPDF
        var Renderer = new ChromePdfRenderer();
        var PDF = Renderer.RenderHtmlAsPdf($"<h1>Report for {currentUser}</h1><p>This is your personalized financial report.</p>");

        // Set file name and content type for the PDF
        var outputFileName = $"Report-{currentUser}.pdf";
        Response.Headers.Add("Content-Disposition", $"attachment; filename={outputFileName}");
        Response.ContentType = "application/pdf";

        // Return the generated PDF file
        return File(PDF.Stream.ToArray(), "application/pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

[Route("api/[controller]")]
[ApiController]
public class ReportController : ControllerBase
{
    [Authorize]
    [HttpGet("download-pdf")]
    public IActionResult DownloadPdfReport()
    {
        // Authentication is handled by DotNetOpenAuth .NET Core
        var currentUser = HttpContext.User.Identity.Name;

        // Generate PDF content using IronPDF
        var Renderer = new ChromePdfRenderer();
        var PDF = Renderer.RenderHtmlAsPdf($"<h1>Report for {currentUser}</h1><p>This is your personalized financial report.</p>");

        // Set file name and content type for the PDF
        var outputFileName = $"Report-{currentUser}.pdf";
        Response.Headers.Add("Content-Disposition", $"attachment; filename={outputFileName}");
        Response.ContentType = "application/pdf";

        // Return the generated PDF file
        return File(PDF.Stream.ToArray(), "application/pdf");
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Imports Microsoft.AspNetCore.Authorization

<Route("api/[controller]")>
<ApiController>
Public Class ReportController
	Inherits ControllerBase

	<Authorize>
	<HttpGet("download-pdf")>
	Public Function DownloadPdfReport() As IActionResult
		' Authentication is handled by DotNetOpenAuth .NET Core
		Dim currentUser = HttpContext.User.Identity.Name

		' Generate PDF content using IronPDF
		Dim Renderer = New ChromePdfRenderer()
		Dim PDF = Renderer.RenderHtmlAsPdf($"<h1>Report for {currentUser}</h1><p>This is your personalized financial report.</p>")

		' Set file name and content type for the PDF
		Dim outputFileName = $"Report-{currentUser}.pdf"
		Response.Headers.Add("Content-Disposition", $"attachment; filename={outputFileName}")
		Response.ContentType = "application/pdf"

		' Return the generated PDF file
		Return File(PDF.Stream.ToArray(), "application/pdf")
	End Function
End Class
$vbLabelText   $csharpLabel

この例では、[Authorize] 属性を使用して、認証されたユーザーのみが PDF 生成エンドポイントにアクセスできるようにします。 IronPDFの ChromePdfRenderer クラスは、HTML コンテンツから PDF を作成するために使用されます。この場合、PDF はユーザーの名前で動的にパーソナライズされます。

DotNetOpenAuth .NET Core (開発者向けの仕組み): 図 1

結論

DotNetOpenAuth .NET CoreとIronPDFの統合は、.NETアプリケーションのセキュリティと機能を向上させる強力なソリューションを提供します。 これらの技術を活用することで、センシティブなデータを効果的に保護し、動的PDF生成を通じて個別のユーザー体験を提供できます。

IronPDFは多用途であるだけでなく、開発者にとっても親しみやすく、.NETアプリケーション内でPDFファイルを作成および管理するための簡単なアプローチを提供します。 IronPDFをプロジェクトに取り入れることを検討している場合は、無料トライアルやライセンスオプションのためにIronPDFの公式Webサイトを探索することをお勧めします。

よくある質問

DotNetOpenAuth .NET Core は何のために使用されますか?

DotNetOpenAuth .NET Core は、.NET アプリケーションに OAuth2 および OpenID 認証機能を統合するために使用され、ウェブおよびデスクトップ環境での安全な認証プロセスを可能にします。

.NET アプリケーションに OAuth2 認証をどのように追加できますか?

OAuth2 認証を追加するには、DotNetOpenAuth .NET Core を使用して WebServerClient をあなたの認可サーバーの詳細で初期化し、その後ユーザーを認証プロセスに導き、認可応答を処理します。

認証を備えた .NET アプリケーションで PDF を作成できますか?

はい、認証に DotNetOpenAuth .NET Core を統合し、PDF 生成に IronPDF を使用することで、個別のレポートや請求書などのセキュアで認証された PDF ドキュメントを作成できます。

.NET アプリケーションでアクセストークンをどのように管理しますか?

DotNetOpenAuth .NET Core は認可応答を処理することでアクセストークンを管理し、安全で認証されたリクエストのためにアクセストークンを取得し利用することを可能にします。

.NET Core でアクセストークンをどのように更新できますか?

.NET Coreでアクセストークンを更新するには、トークンが期限切れかどうかを確認し、`RefreshAuthorization`メソッドを使用して新しいトークンを取得し、継続的な安全なアクセスを確保します。

DotNetOpenAuth と PDF 生成を統合する利点は何ですか?

DotNetOpenAuth と PDF 生成を統合することで、感度の高いドキュメントへの安全なアクセスが可能になり、認証済みユーザーがパーソナライズされた PDF を生成しダウンロードすることができます(レポートや請求書など)。

.NET アプリケーションでトークンをどのように無効にしますか?

トークンを無効にするには、DotNetOpenAuth .NET Core で `RevokeAuthorization` メソッドを実装し、アクセストークンを無効にして、さらなる不正なリクエストを防ぎます。

.NET 認証においてトークンリクエストをどのようにカスタマイズできますか?

トークン処理コードに追加のパラメータを追加することで、認可サーバーの特定の要件を満たすようにトークンリクエストをカスタマイズできます。

.NET プロジェクトに OpenID Connect を設定するにはどのステップが必要ですか?

OpenID Connect を設定するには、必要な権限の詳細で OpenID Connect クライアントを構成し、OpenID プロバイダーを通じた認証をユーザーに促し、シームレスな認証のために DotNetOpenAuth .NET Core との統合を行います。

Jacob Mellor、Ironチームの最高技術責任者(CTO)
最高技術責任者(CTO)

ジェイコブ・メラーはIron Softwareの最高技術責任者(CTO)であり、C# PDFテクノロジーを開拓する先見的なエンジニアです。Iron Softwareのコアコードベースを支えるオリジナル開発者として、彼は創業以来、会社の製品アーキテクチャを形成し、CEOのCameron Rimingtonとともに、会社をNASA、Tesla、および世界的な政府機関にサービスを提供する50人以上の会社に変えました。1999年にロンドンで最初のソフトウェアビジネスを開業し、2005年に最初 for .NETコンポーネントを作成した後、Microsoftのエコシステム全体で複雑な問題を解決することを専門としました。

彼の主要なIronPDFとIron Suite .NETライブラリは、世界中で3000万以上のNuGetインストールを達成し、彼の基礎となるコードは世界中で使用されている開発者ツールに力を与え続けています。25年の商業経験と41年のコーディングの専門知識を持つJacobは、次世代の技術リーダーを指導しながら、エンタープライズグレードのC#、Java、Python PDFテクノロジーにおけるイノベーションの推進に注力しています。

アイアンサポートチーム

私たちは週5日、24時間オンラインで対応しています。
チャット
メール
電話してね