简体   繁体   中英

How to consume and redirect JWT token URL \login\login.php?token=jwt.goes.here using c# web application

I am in the process of creating ASP.net core 2.0 WEB API SSO authentication using JWT. Created a sample Controller which returns the JWT token URL: http://localhost:50311/api/auth/token

  1. How to consume JWT token using C# web application and
  2. How to redirect the page to \\login\\login.php?token=jwt.goes.here in C# web application

Any Advice or best approach to implement this would be a great help.

namespace JWTwebAPI.Controllers
{
    [Route("api/[controller]")]
    public class AuthController : Controller
    {

       [HttpPost("token")]
        public IActionResult Token()
        {
            //string tokenString = "test";
            var claimsdata = new[] { new Claim(ClaimTypes.Name, "username") };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("aabbccddeeffgghh"));
            var signInCred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);
            var token = new JwtSecurityToken(
                issuer: "mysite.com",
                audience: "mysite.com",
                expires: DateTime.Now.AddMinutes(20),
                claims: claimsdata,
                signingCredentials: signInCred
                );
            var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
            return Ok(tokenString);
        }

    }
}

ASP.NEt core web API code:

 [HttpPost("GenerateToken")]
  public IActionResult GenerateToken(string emailid)
    {
   //DB verification for email\username\password goes here  
        if (emailid.Equals("abcd@gmail.com"))
        {
            var claimsdata = new[]
            {
                       new Claim("firstName", "FirstName"),
                       new Claim("LastName", "LasttName"),
                       new Claim("Email", "Email@email.com"),
                       new Claim(ClaimTypes.Email, "myemailid@email.com")
                    };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("mysecretkeygoeshere"));
            var signInCred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken(
                issuer: "mysite.com",
                audience: "mysite.com",
                expires: DateTime.Now.AddMinutes(20),
                claims: claimsdata,
                signingCredentials: signInCred
                );
            var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
            return Ok(new { jwt = tokenString });
        }
        // return BadRequest("Could not verify the user");
        return Unauthorized();

    }

HTML Code goes here

            <script type="text/javascript">
            $(document).ready(function () {
            $("#b1").click(function () {
            var emailid = "abcd@gmail.com";
            $.ajax({                    
                     type:"post",
                     dataType:'json',
                     data:{'emailid': emailid },
                     url: "http://localhost:xxxxx/api/Auth/GenerateToken",                  
                  }).done(function (data) {             
                                             var token = data.jwt
                                             alert(token);                  
                                             url = "xyz.com/login/index.php?
                                             jwt=" + token
                                             $(location).attr("href", url);
                  }).fail( function (error) {
                                              console.log(error);
                                            });

                  });
                });
          </script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM