C# 验证ASP NET API令牌?
背景 Visual Studio 2013,ASP NET WEB API 2 问题 我正在使用.NET的标准成员资格提供程序,但我需要有一个自定义的登录方法。因此,我添加了一个新方法,然后为生成令牌,我使用了以下方法:C# 验证ASP NET API令牌?,c#,.net,asp.net-membership,asp.net-authorization,C#,.net,Asp.net Membership,Asp.net Authorization,背景 Visual Studio 2013,ASP NET WEB API 2 问题 我正在使用.NET的标准成员资格提供程序,但我需要有一个自定义的登录方法。因此,我添加了一个新方法,然后为生成令牌,我使用了以下方法: // Sign-in the user using the OWIN flow var identity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType);
// Sign-in the user using the OWIN flow
var identity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
// This is very important as it will be used to populate the current user id
// that is retrieved with the User.Identity.GetUserId() method inside an API Controller
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id, null, "LOCAL_AUTHORITY"));
AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
var currentUtc = new Microsoft.Owin.Infrastructure.SystemClock().UtcNow;
ticket.Properties.IssuedUtc = currentUtc;
ticket.Properties.ExpiresUtc = currentUtc.Add(new TimeSpan(14, 0, 0, 0));
accesstoken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);
Request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accesstoken);
Authentication.SignIn(identity);
但是当我尝试使用客户端中给出的这个令牌来使用一些授权服务时。我得到了一个不起眼的例外。如何验证此处给出的令牌是否有效
如果不是,我怎样才能生成一个有效的呢?如果你想使用自定义安全流,我想你可以使用authorizeAttribute。 比如: 然后,您可以装饰控制器的方法
[ApiAuthentication]
[Queryable]
public IEnumerable<ProductIndex> GetProductIndex()
{
// Return Data
}
*干杯
[ApiAuthentication]
[Queryable]
public IEnumerable<ProductIndex> GetProductIndex()
{
// Return Data
}