Exemple #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

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

            IEnumerable <Role> roles;
            ApplicationUser    user;

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

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

                //User roles
                roles = (IEnumerable <Role>)_repo.GetRoles(user.Id);
            }

            //if (roles != null) setJson(roles);



            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));

            //Add user roles as Identity claim
            //foreach (Role role in user.Roles)
            //{
            //    identity.AddClaim(new Claim(ClaimTypes.Role, role.id));
            //};


            string jsonroles = JsonConvert.SerializeObject(roles, Formatting.None);

            var props = new AuthenticationProperties(new Dictionary <string, string> {
            });

            props.Dictionary.Add("as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId);
            props.Dictionary.Add("userName", context.UserName);
            props.Dictionary.Add("roles", jsonroles);
            props.Dictionary.Add("userId", user.Id);

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }
Exemple #2
0
        public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
        {
            var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
            var currentClient  = context.ClientId;
            IEnumerable <Role> roles;

            if (originalClient != currentClient)
            {
                context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
                return(Task.FromResult <object>(null));
            }

            using (AuthRepository _repo = new AuthRepository())
            {
                roles = (IEnumerable <Role>)_repo.GetRoles(context.Ticket.Properties.Dictionary["userId"]);
            }

            // Change auth ticket for refresh token requests
            var newIdentity = new ClaimsIdentity(context.Ticket.Identity);

            //Upgrade roles according to Database info
            AuthenticationProperties props = context.Ticket.Properties;

            if (props.Dictionary.ContainsKey("roles"))
            {
                props.Dictionary.Remove("roles");
            }
            string jsonroles = JsonConvert.SerializeObject(roles, Formatting.None);

            props.Dictionary.Add("roles", jsonroles);

            var newTicket = new AuthenticationTicket(newIdentity, props);

            context.Validated(newTicket);

            return(Task.FromResult <object>(null));
        }
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");
            if (allowedOrigin == null) allowedOrigin = "*";
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

            IEnumerable<Role> roles;
            ApplicationUser user;
            using (AuthRepository _repo = new AuthRepository())
            {
                user = await _repo.FindUser(context.UserName, context.Password);

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

                //User roles
                roles = (IEnumerable<Role>)_repo.GetRoles(user.Id);
            }

            //if (roles != null) setJson(roles);



            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));

            //Add user roles as Identity claim
            //foreach (Role role in user.Roles)
            //{
            //    identity.AddClaim(new Claim(ClaimTypes.Role, role.id));
            //};


            string jsonroles = JsonConvert.SerializeObject(roles, Formatting.None);

            var props = new AuthenticationProperties(new Dictionary<string, string> { });
            props.Dictionary.Add("as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId);
            props.Dictionary.Add("userName", context.UserName);
            props.Dictionary.Add("roles", jsonroles);
            props.Dictionary.Add("userId", user.Id);

            var ticket = new AuthenticationTicket(identity, props);
            context.Validated(ticket);
        }
        public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
        {
            var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
            var currentClient = context.ClientId;
            IEnumerable<Role> roles;

            if (originalClient != currentClient)
            {
                context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
                return Task.FromResult<object>(null);
            }

            using (AuthRepository _repo = new AuthRepository())
            {
                roles = (IEnumerable<Role>)_repo.GetRoles(context.Ticket.Properties.Dictionary["userId"]);
            }

            // Change auth ticket for refresh token requests
            var newIdentity = new ClaimsIdentity(context.Ticket.Identity);

            //Upgrade roles according to Database info
            AuthenticationProperties props = context.Ticket.Properties;
            if (props.Dictionary.ContainsKey("roles")) props.Dictionary.Remove("roles");
            string jsonroles = JsonConvert.SerializeObject(roles, Formatting.None);
            props.Dictionary.Add("roles", jsonroles);

            var newTicket = new AuthenticationTicket(newIdentity, props);
            context.Validated(newTicket);

            return Task.FromResult<object>(null);
        }