Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# 现有ASP.NET MVC应用程序中基于令牌的授权_C#_Asp.net Mvc_Asp.net Mvc 3_Asp.net Web Api - Fatal编程技术网

C# 现有ASP.NET MVC应用程序中基于令牌的授权

C# 现有ASP.NET MVC应用程序中基于令牌的授权,c#,asp.net-mvc,asp.net-mvc-3,asp.net-web-api,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Web Api,我继承了一个现有的应用程序。此应用程序使用ASP.NET MVC 3。它有一些API。这些API如下所示: [AcceptVerbs(HttpVerbs.Post)] [Endpoint] public ActionResult AuthenticatePlayer(string username, string password) { // Ensure that the user entered valid credentials if (Membership.ValidateUse

我继承了一个现有的应用程序。此应用程序使用ASP.NET MVC 3。它有一些API。这些API如下所示:

[AcceptVerbs(HttpVerbs.Post)]
[Endpoint]
public ActionResult AuthenticatePlayer(string username, string password)
{
  // Ensure that the user entered valid credentials
  if (Membership.ValidateUser(username, password) == false)
    return Json(new { statusCode = StatusCodes.INVALID_CREDENTIALS, message = "You entered an invalid username or password. Please try again." });


  // Get the profile of the person that just logged in.
  ProfileCommon userProfile = (ProfileCommon)(ProfileCommon.Create(username));
  if (userProfile != null)
  {
    string name = username;
    if (String.IsNullOrEmpty(userProfile.FirstName) == false)
      name = userProfile.FirstName;


    return Json(new {
      statusCode = StatusCodes.SUCCESS,
      payload = name,
      username = username.ToLower(),
    });
  }
}

[AcceptVerbs(HttpVerbs.Get)]
[Endpoint]
public ActionResult SomeUserAction(string q)
{
  // TODO: Ensure the user is authorized to perform this action via a token

  // Do something
  return Json(new { original = q, response = DateTime.UtcNow.Millisecond }, JsonRequestBehavior.AllowGet);
}
我试图找出如何将基于令牌的授权模式集成到这个过程中。根据我的理解,如果用户成功登录,基于令牌的系统将向用户返回一个短期令牌和一个刷新令牌。然后,每个方法都可以通过查看令牌来检查用户是否有权执行该操作。我试图了解这是ASP.NETMVC内置的还是有我可以使用的库。我需要找出最短的方法来完成这件事

非常感谢你

我一年前建立了一个库,提供基于令牌的身份验证:

WebAPI令牌身份验证引导程序是用于WebAPI应用程序的现成基于令牌的用户身份验证,提供随时可用的“令牌授权” 属性和“TokenAuthApicController”控制器

在其功能中-基于令牌的用户身份验证 TokenAuthApicController(Id、用户名、角色、LastAccess)

具有访问权限的基于令牌的用户授权TokenAuthorization属性 级别-公共、用户、管理员或匿名

内置功能Login()、Logoff()、Error()、Unauthorized() 具有各种重载的响应

您可以在的wiki中阅读更多关于和的信息

现在我正在开发一个Node.js应用程序,我正在使用Node.js库使用Json Web令牌(JWT),这非常简单明了。。毕竟是它的Node.js;)


我看到有一个JWT的.NET实现,我建议您看看。

您可以使用Owin。。。i、 e.Microsoft.owin.security

我还没有尝试过这个实现,但这只是给你一个想法:

var identity = new ClaimsIdentity(Startup.OAuthBearerOptions.AuthenticationType);
var currentUtc = new SystemClock().UtcNow;
ticket.Properties.IssuedUtc = currentUtc;
ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); 

return Json(new {
    statusCode = StatusCodes.SUCCESS,
    payload = name,
    username = username.ToLower(),
    accessToken = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket)
});

抱歉问了一个关于基于令牌的身份验证的问题。我曾看到人们在WebAPI项目中选择基于令牌的身份验证,但在使用MVC开发任何网站时,他们几乎不采用这种方法。那么,你能告诉我为什么基于令牌的身份验证是web api的正确解决方案吗?因为人们也可以在web api中使用表单身份验证。谢谢