Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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标识将新实体与其用户关联_C#_Asp.net - Fatal编程技术网

C# Asp标识将新实体与其用户关联

C# Asp标识将新实体与其用户关联,c#,asp.net,C#,Asp.net,我正在使用一个快速演示,其中我使用Asp身份和承载令牌来处理身份验证 我创建了一个评论实体,如下所示: public class CommentModel { public IdentityUser User { get; set; } [Key] public int ID { get; set; } public String Text { get; set; } } 结合上下文: public class AuthContext : IdentityDbC

我正在使用一个快速演示,其中我使用Asp身份和承载令牌来处理身份验证

我创建了一个评论实体,如下所示:

public class CommentModel
{
    public IdentityUser User { get; set; }
    [Key]
    public int ID { get; set; }
    public String Text { get; set; }
}
结合上下文:

public class AuthContext : IdentityDbContext<IdentityUser>
{
    public DbSet<CommentModel> Comments { get; set; }
    public AuthContext() : base("WebDatabase")
    {

    }
}

然后,我创建了一个CommentsController,并试图在用户提交CommentModel时,在post方法中为当前用户创建一个用户字段。但是,当我执行类似这样的操作时,
commentModel.User=wait_repo.FindUser(User.Identity.GetUserName())
传入FindUser的变量始终为null,我也尝试使用
User.Identity.GetUserId()

执行此操作时是否确实登录了?我可以进入该方法,我将[Authorize]属性设置为,这不会阻止我深入了解该方法吗?
User.Identity.GetUserId()是什么意思
return?它返回一个空值。如果您尝试在匿名窗口中浏览,会发生什么情况?
[Authorize]
    // POST: api/Comments
    [ResponseType(typeof(CommentModel))]
    public async Task<IHttpActionResult> PostCommentModel(CommentModel commentModel)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        commentModel.User = await _repo.FindUser(User.Identity.GetUserName());
        db.Comments.Add(commentModel);
        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { id = commentModel.ID }, commentModel);
    }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {

        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        using (AuthRepository _repo = new AuthRepository())
        {
            IdentityUser user = await _repo.FindUser(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
        }

        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("sub", context.UserName));
        identity.AddClaim(new Claim("role", "user"));

        context.Validated(identity);

    }