2014-06-25 4 views
7

Sto fissando un sito Web API e desidero utilizzare i token. Ma, sto lavorando con un database precedente, dove c'è una tabella utenti e ogni utente ha già un token creato per loro e memorizzato nella tabella.Token del portlet API Web: posso utilizzare token personalizzati?

Sto cercando di capire se posso usare il portatore di identità token OAuth bit Auth, ma collegarlo tutto nel mio database esistente, in modo che

  1. concessione di un gettone solo restituisce il token per l'utente dal db
  2. posso validare il token, cercando in su nel db e la creazione di un'identità da parte dell'utente (sto usando ASP.NET identità in altre parti del sito per il lato delle cose MVC)

Non riesco a capire se questo sarà possibile, o se dovrei dare e up e usa un approccio standard per handler HTTP. Ecco il mio codice abbastanza standard finora, che emette solo token standard, non quelli esistenti con cui voglio lavorare.

OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() 
{ 
    AllowInsecureHttp = true, 
    TokenEndpointPath = new PathString("/token"), 
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
    Provider = new SimpleAuthorizationServerProvider() 
}; 

// Token Generation 
app.UseOAuthAuthorizationServer(OAuthServerOptions); 

var bearerAuth = new OAuthBearerAuthenticationOptions() 
{ 
    Provider = new OAuthBearerAuthenticationProvider() 
}; 

app.UseOAuthBearerAuthentication(bearerAuth); 


public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider 
{ 


    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
    { 
     context.Validated(); 
    } 

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 

     context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 

     var manager = new UserManager<User, long>(new UserStore(new UserRepository())); 
     var user = await manager.FindAsync(context.UserName, context.Password); 

     if (user == null) 
     { 
      context.SetError("invalid_grant", "The user name or password is incorrect."); 
     } 
     else 
     { 
      var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
      identity.AddClaim(new Claim("name",user.Email)); 
      context.Validated(identity); 
     } 


    } 
} 

risposta