0

Web Apiでログインと登録にIn-Buildテンプレートを使用しています。web apiで基本認証でログイン

トークンベースの認証を使用します。

しかし、私がしたいことは、私は、Web APIは、今私はAPIメソッドを呼び出すためにするためにそれを使用したい、トークンを返します後、基本認証を使用するユーザーをログインすることです。

今私はこの

var resp = $http({ 
      url: "/TOKEN", 
      method: "POST", 
      data: $.param({ grant_type: 'password', username: userlogin.username, password: userlogin.password }), 
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 
     }); 

のようにログインを要求し、それは私がメソッドを承認呼び出すための次の要求でそれを送った、私にトークンを返します。

しかし、私は、ユーザー名とパスワードを送信するためにワンド:基本認証のよう

HttpClient client = new HttpClient(); 

string authInfo = "admin" + ":" + "123456"; 
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); 
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo); 

client.BaseAddress = new Uri("http://localhost:63173/TOKEN"); 

とサーバー側を分離、私は、ユーザーを検証するために、ユーザー名とパスワードを取得するために戻ってそれを解読し、認証トークンを返します。

var encoding = Encoding.GetEncoding("iso-8859-1"); 
       credentials = encoding.GetString(Convert.FromBase64String(credentials)); 

       int separator = credentials.IndexOf(':'); 
       string name = credentials.Substring(0, separator); 
       string password = credentials.Substring(separator + 1); 

ただし、既存のコードを変更する場所はわかりません。私は考えることができる

+0

サーバ側では、 'Microsoft.Owin.Security.OAuth'と' app.UseOAuthAuthorizationServer'を使用していますか? –

答えて

0

唯一の回避策はOwinパイプラインの先頭で実行さフィールドを、形成するために、Authorizationヘッダーで見つかった資格情報を再マッピングシンプルOwinミドルウェアを書いています。これに似た

何か:

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.Use(async (ctx, n) => 
     { 
      if (ctx.Request.Path == new PathString("/path/to/token")) //the same path you specified in the "TokenEndpointPath" property of "OAuthAuthorizationServerOptions" 
      { 
       var authHeader = ctx.Request.Headers["Authorization"]; 

       if (!string.IsNullOrWhiteSpace(authHeader) && authHeader.StartsWith("Basic ")) 
       { 
        //parse here authHeader 
        var name = GetUsernameFromHeader(authHeader); 
        var password = GetPasswordFromHeader(authHeader); 
        //change the above lines with your parsing methods 

        var form = new Dictionary<string, string[]>() 
        { 
         { "grant_type", new [] {"password" }}, 
         { "username", new []{ name }}, 
         { "password", new [] { password}} 
        }; 

        ctx.Set<IFormCollection>("Microsoft.Owin.Form#collection", new FormCollection(form)); 
       } 
      } 

      await n.Invoke(); 
     }); 


     //app.UseWebApi... 
     //other middlewares 
    } 
} 

私はこのアプローチはちょうどOwinコンテキストにそれらの値を書き込み、限り、基礎となるOAuthAuthorizationServerMiddleware実装が変更されないように動作することを警告。

+0

しかし、 'GrantResourceOwnerCredentials'メソッドで' context.ClientId'にアクセスしようとするとnullが返されます。ここで私は '{" client_id "、new [] {clientId}}'のようにこの 'StartUp'を設定しました。 また、基本認証でclientIdを送信しています。 – Programmer

+0

私はあなたの質問に答えましたが、これは決してclient_idを言及しませんでした。 (OAuthValidateClientAuthenticationContextで 'TryGetFormCredentials'メソッドを使用して)' OnValidateClientAuthentication'プロパティを提供することで最初に解析して検証しないと、 'GrantResourceOwnerCredentials'のコンテキストからclient_idを取得できません。 –

+0

ありがとうございます。私はClientIdを設定する方法を考え出しました。 'ValidateClientAuthentication'メソッドの ' var getClient = context.Parameters ["client_id"]; context.OwinContext.Set ( "as:client_Id"、clientId); context.Validated(clientId); ' 私は' context.ClientId'を取得できません – Programmer