asp net core identity bearer token authentication example

[HttpPost]
public async Task<IActionResult> AccessToken([FromForm]string code, [FromForm]string grant_type, [FromForm]string redirect_uri, [FromForm]string client_id, [FromForm]string client_secret)
{
    // Check if code is correct and if client credentials are correct.
    if (grant_type != "authorization_code")
    {
        return Redirect(redirect_uri + "?error=unsupported_response_type");
    }
    
    if (!clientValidator.Valid(client_id, client_secret))
    {
        return Redirect(redirect_uri + "?error=access_denied");
    }
    
    // Extract the URI and user
    string previous_uri = codeAndURIStorage.Load(code);
    string user = codeAndUserStorage.Load(code);
    codeAndURIStorage.Delete(code);
    codeAndUserStorage.Delete(code);

    // Check if the new uir is the same as the previous and that the userId was found
    if (redirect_uri != previous_uri)
    {
        return BadRequest("'redirect_uri' was inconsistent.");
    }
    
    if (user == null)
    {
        return BadRequest("Couldn't find user associated with the given code.");
    }

    // Creates the signed JWT
    var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["TokenOptions:Key"]))
    var tokenHandler = new JwtSecurityTokenHandler();
    var tokenDescriptor = new SecurityTokenDescriptor
    {
        Subject = new ClaimsIdentity(new Claim[]
        {
            new Claim(ClaimTypes.Name, user)
        }),
        Expires = DateTime.UtcNow.AddYears(2),
        Issuer = "MyWebsite.com",
        Audience = "MyWebsite.com",
        SigningCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256Signature)
    };
    var token = tokenHandler.CreateToken(tokenDescriptor);
    var access_token = tokenHandler.WriteToken(token);

    // Returns the 'access_token' and the type in lower case
    return Ok(new { access_token, token_type="bearer" });
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
how to sign out of bearer token asp.net core login authentication asp net core jwt bearer asp net core identity api add jwt token asp net core api add bearer token authentication ASP.NET Core Token Authentication asp net core get token from authentication header how to get userid from bearer token in asp.net core net core api bearer token authentication Token Based Authentication using ASP.NET core Web API , Owin, and Identity asp net core 5 bearer token authentication create new net core web api with identity authentication asp.net core identity tokens identity with token asp.net core application net core 5 token oauth bearer token based authentication example Asp net Core Identity token provider token based authentication asp.net core create token based web api c# in .net core using identity JWT Token Authentication in REST API Using ASP.NET Core Identity .net core add bearer token asp net core add authentication bearer creating bearer token authentication .net core create a api server with bearer token authetication .net Core asp.net identity with jwt bearer asp.net core authorization bearer asp net core 5 bearer token authorization asp net core bearer token example net core rest api bearer token authentication apply bearer token asp.net core asp.net core identity token using asp net core identity for token based authentication asp.net core identity support token .net core identityserver bearer token add bearer authorization asp net core reading authentication bearer token api from asp.net core azure ad authentication web api .net core net core 3.1 identity token based authentication token based authentication using asp.net core web api net core add bearer token authentication dotnet core jwt user openid create a .NET Core API with Bearer Authentication dotnet core bearer token web application with bearer token .net core .net core bearer token authentication how to set token server side .net core set token .net core mvc set token header .net core asp net core web api bearer token authentication asp net core web api check token authentication .net core bear token asp net user tokens authentication without identity asp.net core openidconnect asp.net core with bearer .net core bearer token bearer token .net core OAuth 2.0 Bearer token .net core asp net core web api authentication token example OpenId validate token in service dotnet core asp.net core web api authentication and authorization .net core token netlify Authorization: Bearer identity asp.net core identity token authentication asp.net core identity web api jwt asp net core identity update cookie authentication and jwt bearer token authentication tutorial asp.net core get token from header choose jwtvalidation .net core middleware on issuer dotnet core &quot;jwks_uri&quot; token validation dotnet core jwks_uri token validation .net core add bearer token to header .net core validate token jwk_uri exapmle how to store the auth token in asp.net core tokenvalidationparameters .net core access in controller .net core identity server jwt get id from token in dotnet core get user token in ,net core 3.1 move jwt authentication code from startup in middleware core net asp.net core 5 JwtBearer validate toekn url asp.net core 5 authorization URL asp.net core middleware for token jwt bearer token asp.net core .net core owin oauth c# .net core 3 how to use access token How to check AsPNetCore.identity tokens asp.net core with jwt token authentication without identity .net framework validate jwt on each request how to get access token in asp.net core asp.netusertokens best practices dotnet core get ayth token from header dotnet core get ayth token jwt bearer token c# .net core web api validate token api login with access token in net core login with access token in net core dotnet core validate jwt client key and secret set bearer token in core .net .net [authorize] with token asp.net core api authentication using jwt bearer tokens jwt token issuer audience .net core example asp net core get data from token .Net Core generate oauth token ASP.net token how to change token generation for asp.net core generate a jtw token without identity mobile authentication with bearer in .net core add token authentication to aspnetcore project .net token authentication .net core read token value .net core token value how to receive token asp.net asp net core how to store security token service net core add bearer in middleware asp net core allow bearer token authentication middleware for token validation dot dotnet core asp.net core token based authentication ASP.NET core tutorial working with tokens token server asp.net core oauthbearerauthenticationmiddleware asp.net core example OAuthBearerAuthenticationMiddleware .net core what is token in asp.net core .net core token based authentication how to info from auth token in .net core how to get auth token from header in .net core .net core token based authorization asp.net core bearer token authentication OAuth 2.0 Bearer Access Token Generation asp.net core .net core auth token and secret .net core mvc, how to access jwt token sing in generate token options asp.net core asp net core bearer token authentication .net oidc bearer access token .net oidc bearer not jwt .net validate access token assign jwt bearer token asp net core how to validate autho token in backend c# diff between token key and access code in api asp.net core how to use bearer token asp.net core authentication using token in asp.net core enable token authentication asp.net .net core web api bearer token authentication asp.net core mvc bearer token
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source