如何保存GrantResourceOwnerCredentials方法将标记值返回到数据库

时间:2016-08-30 14:32:18

标签: asp.net asp.net-web-api owin

我试图通过使用Owin来实现Oauth。如下所示,我想在我的数据库中存储生成的令牌。我不知道如何/在哪里采取令牌值。我可以在GrantResourceOwnerCredentials方法中处理它还是有任何其他方法可以覆盖?

  public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {

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


        using (EvdekiHesapContext _repo = new EvdekiHesapContext())
        {
            User user = null;
            try
            {
                user = _repo.Users.Where(x => x.UserName == context.UserName && x.PassWord == context.Password).FirstOrDefault();
            }
            catch (Exception ex)
            {

                throw;
            }


            if (user != null)
            {
                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim("UserName", user.UserName));
                identity.AddClaim(new Claim("role", "user"));

                var ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
                context.Validated(ticket);
                // user.Token = ???  At this point , I want to save the genereted token to the DB. ??
                _repo.SaveChanges();
            }
            else
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
        }

    }

感谢。

0 个答案:

没有答案