Ejemplo n.º 1
0
        public override async Task GrantResourceOwnerCredentials
            (OAuthGrantResourceOwnerCredentialsContext context)
        {
            User entry = new User();

            using (FineArtEntities fineArtEntities = new FineArtEntities())
            {
                entry = fineArtEntities.Users.Where
                        <User>(u =>
                               u.Username == context.UserName &&
                               u.Password == context.Password).FirstOrDefault();

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

            ClaimsIdentity oAuthIdentity =
                new ClaimsIdentity(context.Options.AuthenticationType);

            oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            ClaimsIdentity cookiesIdentity =
                new ClaimsIdentity(context.Options.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(entry.UserId);
            AuthenticationTicket     ticket     =
                new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }