Exemplo n.º 1
0
            public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

                //Check User
                AuthBL       rep  = new AuthBL();
                IdentityUser user = rep.FindUser(context.UserName, context.Password);

                //create toke
                if (user == null)
                {
                    context.SetError("User Not Found");
                }
                else
                {
                    ClaimsIdentity identity = new ClaimsIdentity(context.Options.AuthenticationType);
                    identity.AddClaim(new Claim("UserName", context.UserName));
                    if (rep.FinduserRole(context.UserName, context.Password) == "Admin")
                    {
                        identity.AddClaim(new Claim(identity.RoleClaimType, "Admin"));
                    }

                    else if (rep.FinduserRole(context.UserName, context.Password) == "User")
                    {
                        identity.AddClaim(new Claim(identity.RoleClaimType, "User"));
                    }

                    context.Validated(identity);
                }
            }
        public IHttpActionResult Login(LoginModel user)
        {
            AuthBL rep = new AuthBL();

            ApplicationUser res = rep.FindUser(user.UserName, user.Password);

            if (res != null)
            {
                if (res.IsDeleted == false)
                {
                    return(Ok(rep.FinduserRole(user.UserName, user.Password)));
                }
            }
            return(NotFound());
        }