2016-12-17 8 views
1

私はAsp.net web api 2プロジェクトを持っています。このプロジェクトで私はOWIN認証を使用します。 私は2種類のユーザーを持っています。owinコンテキストでWeb api 2のユーザーのベアラトークンを作成するには2通りの方法を使用してください。

ユーザー名とパスワードでログインするユーザーと、携帯電話番号と4文字の単語でログインするユーザーの2種類があります。私はこれまでのところ、私の実装で、これらのユーザーの両方が自分のトークンを取得するためにトークンアドレスを/行きたい

は、このようなものです:

これは、起動クラスである:

var provider = new AuthorizationServerProvider(); 
var options = new OAuthAuthorizationServerOptions() 
{ 
    AllowInsecureHttp = true, 
    TokenEndpointPath = new PathString("/token"), 
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
    Provider = provider 
}; 
public class AuthorizationServerProvider : OAuthAuthorizationServerProvider 
{ 
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
    { 
     context.Validated(); 
    } 

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
     using (DbContext dbContext = new DbContext()) 
     { 
      var user = dbContext.User 
       .Where(a => a.UserName == context.UserName) 
       .Where(a => a.Password == context.Password) 
       .Select(a => new UserClaim 
       { 
        Id = a.Id, 
        UserName = a.UserName, 
        FirstName = a.FirstName, 
        LastName = a.LastName, 
        Roles = a.UserInRoles.Select(w => w.Role.Id).ToList() 
       }).FirstOrDefault(); 

      if (user == null) 
      { 
       context.SetError("invalid grant", "Provided username and password is incorrect."); 
       return; 
      } 
      identity.AddUserClaim(user); 
      context.Validated(identity); 
      return; 
     } 
    } 
} 

このソリューションは、ユーザーのためにあります誰がユーザー名でログインしたいのですが、モバイル番号でログインしたいユーザーはどうすればいいですか?

+1

複数の 'OAuthAuthorizationServerProvider'をパイプラインに追加できるはずです。ユーザー名/パスワード用とモバイル/ピン用の2つを作成してください。各プロバイダで、どちらか一方が使用されているかどうかを確認します。 – Michael

答えて

1

OAuthAuthorizationServerOptionsの2つのインスタンスを提供する必要があります。ユーザー名とパスワードを許可する場合とmobileNumberとコードを許可する場合があります。この2つのオプションは、認証ミドルウェアを介してowinパイプラインに追加してください。

public partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     // rest of your code 

     var userAndPasswordOptions = new OAuthAuthorizationServerOptions(){ ... }; 
     var mobileAndCodeOptions = new OAuthAuthorizationServerOptions(){ ... }; 

     app.UseOAuthAuthorizationServer(userAndPasswordOptions); 
     app.UseOAuthAuthorizationServer(mobileAndCodeOptions); 

     // rest of your code 
    } 
} 

この場合、これらの2つのプロバイダが異なるリクエストエンドポイントに応答することがわかります。

両方のタイプの認証を提供するために1つのエンドポイントが必要な場合は、OAuthAuthorizationServerProviderGrantResourceOwnerCredentialsメソッドを変更することができます。

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
{ 
    var identity = new ClaimsIdentity(context.Options.AuthenticationType); 

    var form = await context.Request.ReadFormAsync().Result; 
    if (form["type"] == "mobile") 
    { 
     //validate mobileNumber and code 
    } 
    else 
    { 
     //validate username and password 
    } 

    identity.AddUserClaim(user); 
    context.Validated(identity); 
    return; 

}