Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#web应用程序使用和重定向JWT令牌URL\login\login.php?token=JWT.goes.here_C#_Asp.net Core_Jwt - Fatal编程技术网

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

如何使用c#web应用程序使用和重定向JWT令牌URL\login\login.php?token=JWT.goes.here,c#,asp.net-core,jwt,C#,Asp.net Core,Jwt,我正在使用JWT创建ASP.net core 2.0 WEB API SSO身份验证。创建了一个返回JWT令牌URL的示例控制器: 如何使用C#web应用程序和 如何在C#web应用程序中将页面重定向到\login\login.php?token=jwt.goes.here 任何实现这一点的建议或最佳方法都将大有裨益 namespace JWTwebAPI.Controllers { [Route("api/[controller]")] public class AuthCon

我正在使用JWT创建ASP.net core 2.0 WEB API SSO身份验证。创建了一个返回JWT令牌URL的示例控制器:

  • 如何使用C#web应用程序和
  • 如何在C#web应用程序中将页面重定向到\login\login.php?token=jwt.goes.here
  • 任何实现这一点的建议或最佳方法都将大有裨益

    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核心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代码在这里

                <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> 
    
    
    $(文档).ready(函数(){
    $(“#b1”)。单击(函数(){
    var emailid=”abcd@gmail.com";
    $.ajax({
    类型:“post”,
    数据类型:'json',
    数据:{'emailid':emailid},
    url:“http://localhost:xxxxx/api/Auth/GenerateToken",                  
    }).done(函数(数据){
    var token=data.jwt
    警报(令牌);
    url=“xyz.com/login/index.php?
    jwt=“+令牌
    $(location.attr(“href”,url);
    }).失败(功能(错误){
    console.log(错误);
    });
    });
    });
    
    为什么要将web api重定向到登录页面?嗨,Brad,在C#web应用程序中使用JWT令牌并重定向到\login\login.php?令牌=JWT.goes.here在web应用程序中,而不是在web api中