Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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 API令牌?_C#_.net_Asp.net Membership_Asp.net Authorization - Fatal编程技术网

C# 验证ASP NET API令牌?

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

背景

Visual Studio 2013,ASP NET WEB API 2

问题

我正在使用.NET的标准成员资格提供程序,但我需要有一个自定义的登录方法。因此,我添加了一个新方法,然后为生成令牌,我使用了以下方法:

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