public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
            var userManager = new UserServices(new ZenergyContext());

            //ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
            user user = await userManager.findByMailAndPassword(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "Le nom d'utilisateur ou le mot de passe est incorrect.");
                return;
            }

            /*ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
               OAuthDefaults.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                CookieAuthenticationDefaults.AuthenticationType);*/

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim("mail", context.UserName));
            identity.AddClaim(new Claim("role", "user"));
            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim("UserId", user.userId.ToString()));
            identity.AddClaim(new Claim(ClaimTypes.Role, "User"));

            if (user.member != null)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, "Member"));
            }
            if (user.contributor != null)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, "Contributor"));
            }
            if (user.manager != null)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, "Manager"));
            }
            if (user.admin != null)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
            }      

            AuthenticationProperties properties = CreateProperties(user.userId.ToString(), user.mail);
            AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);
            context.Validated(ticket);
            //context.Request.Context.Authentication.SignIn(cookiesIdentity);
            context.Request.Context.Authentication.SignIn(identity);
        }
예제 #2
0
 public AccountController()
 {
     userServices = new UserServices(db);
 }
예제 #3
0
 public usersController()
 {
     userServices = new UserServices(db);
 }