C# 使用OAuth2授权Web API 2.2中不起作用的属性

C# 使用OAuth2授权Web API 2.2中不起作用的属性,c#,asp.net-web-api,oauth,oauth-2.0,C#,Asp.net Web Api,Oauth,Oauth 2.0,我正在放置一个[授权(用户=”user@sample.com“”]我的控制器顶部的属性: [Authorize(Users = @"user@sample.com")] public class PostsController : ApiController { ... methods... } 用户的用户名是电子邮件地址。当我使用user@sample.com我仍然得到了一个关于此控制器方法的许可证 我还注意到,即使用户在System.Web.HttpContext.Current.u

我正在放置一个
[授权(用户=”user@sample.com“”]
我的控制器顶部的属性:

[Authorize(Users = @"user@sample.com")]
public class PostsController : ApiController
{
   ... methods...
}
用户的用户名是电子邮件地址。当我使用
user@sample.com
我仍然得到了一个关于此控制器方法的许可证

我还注意到,即使用户在
System.Web.HttpContext.Current.user.Identity.Name
System.Security.Claims.ClaimsPrincipal.Current.Identity.Name
中签名后,仍然是
null

以下是几个相关的类别:

ApplicationUser.cs
公共类应用程序用户:IdentityUser
{
公共字符串{get;set;}
公共异步任务GenerateUserIdentityAsync(UserManager管理器,字符串身份验证类型)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是authenticationType);
//在此处添加自定义用户声明
返回用户身份;
}
}
应用程序上下文
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“MyProjectEntityIdentity”,throwifvv1schema:false)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
}
这个问题发生在我的本地dev实例以及托管站点上


我知道这是非常模糊的,我只是不知道还有什么其他问题可能与此相关。所以,请询问更多信息,我会更新帖子

您是否尝试过将用户名更改为简单的用户名,例如paul。。。换句话说,试着删除@符号和句号?不。我真的很想了解
[Authorize(Users=“”)]
在做什么。忘记了,@符号应该没问题。。。这篇文章可能会对你有帮助。。。查看自定义属性。。。实施它。。。然后调试它。。。找到你的问题。这是我的帖子。哈哈,我正试图避免所有这些。。。
public class ApplicationUser : IdentityUser
{
    public string Hometown { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        // Add custom user claims here
        return userIdentity;
    }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MyProjectEntitiesIdentity", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}