I was not very clear about your doubt, but there is no need for a if
to compare the token, own framework does so internally.
Example:
In his startup.cs
, you will probably have a method similar to this:
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
This method defines the endpoint and the time that the token will last
You must also have one preview sort of like this
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[] { "*" });
using (IUserRepository _repository = new UserRepository(new Data.DataContexts.OAuthServerDataContext()))
{
var user = _repository.Authenticate(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
}
To authenticate you need to make a request on this endpoint passing the three information
- grant_type
- username
- password
After that you will receive as a reply token
At last, in all action which is decorated with the [Authorize] will require the token to be in the header request, with the attribute Authorization
If the token is invalid returns Forbbiden 403.
Obs: the token is composed of information and we can both add and capture such information, note that in the block identity.AddClaim(new Claim("role", "user"));
is added the userin the token
that was my question. if in [Authorize] he already validated this token. thank you.
– Guilherme Camarotto