コード例 #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            using (var db = new LovNaZakladDbContext())
            {
                var user = db.Users.SingleOrDefault(u => u.Username == context.UserName);
                if (Crypto.VerifyHashedPassword(user.Password, context.Password))
                {
                    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                    identity.AddClaims(new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, user.Username),
                        new Claim(ClaimTypes.Email, user.Email),
                        new Claim(ClaimTypes.PrimarySid, user.UserID.ToString())
                    });

                    if (context.Scope.Count != 0)
                    {
                        identity.AddClaims(context.Scope.First()?.Split(',')?.Select(s => new Claim("as:scope", s)));
                    }

                    var properties = new AuthenticationProperties(new Dictionary <string, string> {
                        { "client_id", context.ClientId },
                        { "username", context.UserName }
                    });

                    var ticket = new AuthenticationTicket(identity, properties);
                    context.Validated(ticket);
                }
                else
                {
                    context.Rejected();
                    context.SetError("invalid_grant", "Username or Password is not correct");
                }
            }
        }