C# Web api如何使用从api发送的令牌
我已经挖了三天了,仍然找不到一个好的答案。如果有人能帮我,我真的很感激 例如客户端使用登录功能从www.client.com登录到 我的web api已成功验证并向用户发送令牌。客户端如何使用api返回的令牌访问方法C# Web api如何使用从api发送的令牌,c#,asp.net-web-api,asp.net-ajax,C#,Asp.net Web Api,Asp.net Ajax,我已经挖了三天了,仍然找不到一个好的答案。如果有人能帮我,我真的很感激 例如客户端使用登录功能从www.client.com登录到 我的web api已成功验证并向用户发送令牌。客户端如何使用api返回的令牌访问方法 [RoutePrefix("api/Customer")] public class CustomerController : ApiController { List<customer> list = new List<customer>() {
[RoutePrefix("api/Customer")]
public class CustomerController : ApiController
{
List<customer> list = new List<customer>() { new customer {id=1 ,customerName="Marry",age=13},
new customer { id = 2, customerName = "John", age = 24 } };
[Route("GetExployeeByID/{id:long}")]
[HttpGet]
[Authorize]
public customer GetExployeeByID(long id)
{
return list.FirstOrDefault(x => x.id == id);
}
}
属性方法。客户端正在跨域使用Ajax调用该方法,my webapi已经在web.config的webapi配置和cros策略中打开了CRO,当他们发送请求时,他们应该添加一个名为“Authorization”的头以及您的令牌值
然后,当请求到来时,您可以将其从标头中拉出,并处理身份验证控制当他们发送请求时,他们应该添加一个名为“Authorization”的标头以及您的令牌值
然后,当请求到来时,您可以将其从标头中拉出并处理身份验证控制您应该编写一个自定义的
授权属性
,如下所示:
public class CheckAttendeeNameAttribute : System.Web.DomainServices.AuthorizationAttribute
{
public override bool Authorize(System.Security.Principal.IPrincipal principal)
{
if (principal.IsInRole("Attendee") && principal.Identity.Name.StartsWith("A"))
{
return true;
}
else
{
return false;
}
}
}
您应该编写一个自定义的
AuthorizationAttribute
,如下所示:
public class CheckAttendeeNameAttribute : System.Web.DomainServices.AuthorizationAttribute
{
public override bool Authorize(System.Security.Principal.IPrincipal principal)
{
if (principal.IsInRole("Attendee") && principal.Identity.Name.StartsWith("A"))
{
return true;
}
else
{
return false;
}
}
}
一旦用户通过身份验证,他可以在请求头中发送令牌,您可以在
Authorize
过滤器中检查请求头,如以下代码所示:
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication1.Models
{
public class AuthorizeFilter : AuthorizeAttribute
{
public bool verifyToken(string token)
{
return false;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// Get the headers
var headers = httpContext.Request.Headers;
//your token verification
if (verifyToken(headers["SomeHeader"]))
{
return true;
}
return false;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
try
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new { controller = "Home", action = "NotAuthorzied" }));
}
catch (Exception ex)
{
}
}
}
}
一旦用户通过身份验证,他可以在请求头中发送令牌,您可以在
Authorize
过滤器中检查请求头,如以下代码所示:
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebApplication1.Models
{
public class AuthorizeFilter : AuthorizeAttribute
{
public bool verifyToken(string token)
{
return false;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// Get the headers
var headers = httpContext.Request.Headers;
//your token verification
if (verifyToken(headers["SomeHeader"]))
{
return true;
}
return false;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
try
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new { controller = "Home", action = "NotAuthorzied" }));
}
catch (Exception ex)
{
}
}
}
}
尝试以下方法:将其用作控制器方法上的筛选器
public class AuthorizationFilter : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var isAuthenticated = base.IsAuthorized(actionContext);
if (isAuthenticated)
{
var headers = actionContext.Request.Headers;
IEnumerable<string> header;
headers.TryGetValues("AuthorizationHeaderName", out header);
var token = header.GetEnumerator().Current;
//validate your token
if (tokenVerification(token))
{
return true;
}
return false;
}
}
private bool tokenVerification (string token)
{
if // valid token
return true;
else return false;
}
}
公共类AuthorizationFilter:authorizationAttribute
{
受保护的覆盖布尔已授权(HttpActionContext actionContext)
{
var isAuthenticated=base.isAuthenticated(actionContext);
如果(未经验证)
{
var headers=actionContext.Request.headers;
IEnumerable头;
headers.TryGetValues(“AuthorizationHeaderName”,out header);
var token=header.GetEnumerator().Current;
//验证您的令牌
if(令牌验证(令牌))
{
返回true;
}
返回false;
}
}
私有布尔令牌验证(字符串令牌)
{
如果//有效令牌
返回true;
否则返回false;
}
}
尝试以下方法:将其用作控制器方法上的过滤器
public class AuthorizationFilter : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var isAuthenticated = base.IsAuthorized(actionContext);
if (isAuthenticated)
{
var headers = actionContext.Request.Headers;
IEnumerable<string> header;
headers.TryGetValues("AuthorizationHeaderName", out header);
var token = header.GetEnumerator().Current;
//validate your token
if (tokenVerification(token))
{
return true;
}
return false;
}
}
private bool tokenVerification (string token)
{
if // valid token
return true;
else return false;
}
}
公共类AuthorizationFilter:authorizationAttribute
{
受保护的覆盖布尔已授权(HttpActionContext actionContext)
{
var isAuthenticated=base.isAuthenticated(actionContext);
如果(未经验证)
{
var headers=actionContext.Request.headers;
IEnumerable头;
headers.TryGetValues(“AuthorizationHeaderName”,out header);
var token=header.GetEnumerator().Current;
//验证您的令牌
if(令牌验证(令牌))
{
返回true;
}
返回false;
}
}
私有布尔令牌验证(字符串令牌)
{
如果//有效令牌
返回true;
否则返回false;
}
}
我是否在方法本身中提取标题?意味着我的方法应该有一个header/URL参数?但是它将不允许访问该方法,因为它没有被授权。不,您将头放在请求中。请求应添加标题。当请求到达时,您应该检查标题。这可以通过全局(基本)ApiController或FilterAttribute实现。如果我将令牌添加到httpOnly cookie中,您知道我如何在发送请求后读取和设置令牌服务器端我在方法本身中提取头吗?意味着我的方法应该有一个header/URL参数?但是它将不允许访问该方法,因为它没有被授权。不,您将头放在请求中。请求应添加标题。当请求到达时,您应该检查标题。这可以通过全局(基本)ApiController或FilterAttribute实现。如果我将令牌添加到httpOnly cookie中,您知道我如何在发送请求后读取和设置令牌服务器端吗?我复制了代码,它显示错误,表示找不到域服务。我更改了我的命名空间,但显示了相同的错误。请参见:我复制了代码,它显示了一个错误,表示找不到域服务。我更改了名称空间,但显示了相同的错误。请参见:我无法重写此方法。在授权属性中找不到。我已更新了我的原始帖子。使用控制器上的文件管理器。[AuthorizeFilter]public ActionResult Index(){return View();}请告诉我它是否有效。我无法重写此方法。在授权属性中找不到。我已更新了我的原始帖子。使用控制器上的文件管理器。[AuthorizeFilter]public ActionResult Index(){return View();}请告诉我它是否有效。