简体   繁体   English

如何使用C#Web应用程序使用和重定向JWT令牌URL \\ login \\ login.php?token = jwt.goes.here

[英]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. 我正在使用JWT创建ASP.net core 2.0 WEB API SSO身份验证。 Created a sample Controller which returns the JWT token URL: http://localhost:50311/api/auth/token 创建了一个示例控制器,该控制器返回JWT令牌URL: http:// localhost:50311 / api / auth / token

  1. How to consume JWT token using C# web application and 如何使用C#Web应用程序使用JWT令牌和
  2. How to redirect the page to \\login\\login.php?token=jwt.goes.here in C# web application 如何在C#Web应用程序中将页面重定向到\\ login \\ login.php?token = jwt.goes.here

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: ASP.NEt核心Web API代码:

 [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 HTML代码在这里

            <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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