public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {

           user = Mapper.Map<UserModel>(await userService.FindAsync(context.UserName, context.Password));

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
            else
            {
                ClaimsIdentity identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim("sub", context.UserName));
                identity.AddClaim(new Claim("role", "user"));

                // Proporties
                IDictionary<string, string> prop = new Dictionary<string, string>()
                {
                    { "username", user.UserName },
                    { "id", user.Id}
                };

                // Add dictionary to auth proporties
                AuthenticationProperties proporties = new AuthenticationProperties(prop);

                AuthenticationTicket ticket = new AuthenticationTicket(identity, proporties);
                
                context.Validated(ticket);
            }

        }
Beispiel #2
0
        public async Task<HttpResponseMessage> Get(string username)
        {
            try
            {
                UserModel user = Mapper.Map<UserModel>(await userService.FindAsync(username));
                userForValidation = user;

                if (user == null)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound, "Can't find user with given id");
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.OK, user);
                }
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }