9

この例では、ユーザーが (MVC 5) Web アプリケーションにログインし、プロキシとして機能して (Web API 2) API (Basic over SSL を使用) にログインし、ベアラー/アクセス トークンを返します。私は Thinktecture.IdentityModel.Client.OAuth2Client を使用してログインを処理し、アクセス トークンを取得していますが、これはすべて正常に機能します。

他にもいくつかのことが起こりますが、Web アプリケーションがアクセス トークンをデコードして、API に設定されたクレーム (具体的には、API へのログイン後に返されるユーザー ID) にアクセスできるようにしたいと考えています。

私は多くのデモを使用しており、トークンエンドポイントを使用して拡張メソッドを使用していますが、独自のリポジトリにアクセスするためのカスタムがありUseOAuthAuthorizationServerます。UseOAuthBearerAuthenticationOAuthAuthorizationServerOptions.Provider

両方のアプリケーションで同じ MachineKey を使用していますが、トークンをデコードする方法が不明ですが、おそらくSecureDataFormat.Unprotectメソッドを使用する必要があることは理解しています。

私が Web アプリケーションで行った最も近い試みは次のとおりです。

Task<TokenResponse> response = client.RequestResourceOwnerPasswordAsync(model.Email, model.Password);

IDataProtector dataProtecter = Startup.DataProtectionProvider.Create("does this matter?");
TicketDataFormat ticketDataFormat = new TicketDataFormat(dataProtecter);
AuthenticationTicket ticket = ticketDataFormat.Unprotect(response.Result.AccessToken);

Startup.DataProtectionProvider を次のように設定します。

public partial class Startup
{
    internal static IDataProtectionProvider DataProtectionProvider { get; private set; }

    public void Configuration(IAppBuilder app)
    {
        DataProtectionProvider = app.GetDataProtectionProvider();
        this.ConfigureAuth(app);
    }
}

私のフォールバック計画は、ログイン後に興味のある情報を返す API メソッドを提供することですが、それがトークンのクレームの一部を形成するため (私が理解しているように)、過剰に見えます。

私は JWT について頭を悩ませようとしました (Thinktecture、Microsoft のソース コード、およびその他のさまざまなフォーラムを見てきました) が、それが役立つかどうかはわかりません (ただし、プレーン テキストで利用できる主張は役に立ちます)。基本認証でのサインインを許可し、アクセス トークンを含むカスタム JWT を返す例をまだ見つけていません。

とにかく、それが十分な情報であることを願っています。どんな助けでも大歓迎です...乾杯

4

2 に答える 2

4

あなたたちはとても近づきました。シャディは正しい軌道に乗っていました。これが別の mvc アプリケーションであっても、示されているようにトークンを復号化し、クレームを抽出する必要があります。Web API トークンが accessToken という変数にある場合、mvc Web アプリで次の操作を実行できます。(mvc はベアラー認証と Cookie 認証の両方を使用しており、OAuthBearerOptions は静的クラスであることに注意してください)

//Unprotect the token 
var unencryptedToken = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(accessToken);
//Extract the claims identity from the token
var identity = unencryptedToken.Identity;

//Once you have the claims identity extracted, create a new claims identity that uses 
//ApplicationCookie as the default authentication type

var id = new ClaimsIdentity(identity.Claims, DefaultAuthenticationTypes.ApplicationCookie);

// Now you are ready to sign in with your new claims identity. You basically created an  
//application cookie from a bearer token and now using this cookie to sign-in

AuthenticationManager.SignIn(id);

以下は、startup.auth.cs に含める方法です (OAuthBearerAuthenticationOptions 静的メンバーがあり、ベアラー トークンを復号化できるようにするためだけに app.UseOAuthBearerAuthentication() を呼び出すことに注意してください)。

public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }

    static Startup()
    {
        OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
    }


    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseOAuthBearerAuthentication(OAuthBearerOptions);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
    }
于 2014-12-29T22:11:24.327 に答える
1

UseOAuthBearerAuthentication を使用している場合は、Startup.Auth.cs クラスで OAuthBearerOptions を静的にすることができます。

public partial class Startup
{
    public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
    ...

    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per    request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
        app.UseOAuthBearerAuthentication(OAuthBearerOptions);
        ...

    }
}

コードのどこからでも、次の方法で受け取ったトークンの保護を解除できます。

var ticket = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(response.Result.AccessToken);

これで、API ユーザーのクレームにアクセスするためのチケットを使用できます。

ticket.Identity.Claims

これがあなたの質問に答えることを願っています。

編集

SOに関するこの回答はあなたの問題を解決しますので、ご覧ください。

于 2014-10-26T13:34:39.870 に答える