예제 #1
0
        public IHttpActionResult Get()
        {
            try {
                using (hlaplusEntities db = new hlaplusEntities())
                {
                    UserAccount user = new UserAccount();

                    user = db.UserAccounts.Where(u => u.Email == "*****@*****.**").FirstOrDefault();



                    if (user == null)
                    {
                        return(Json("Error"));
                    }
                    else
                    {
                        return(Json("Web API was sucessfully Tested..v2.2"));
                    }
                }
            }catch (Exception ex)
            {
                return(Json(ex));
            }
        }
예제 #2
0
        public override async Task GrantResourceOwnerCredentials(Microsoft.Owin.Security.OAuth.OAuthGrantResourceOwnerCredentialsContext context)
        {
            try
            {
                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });


                using (hlaplusEntities con = new hlaplusEntities())
                {
                    var user = con.UserAccounts.Where(u => u.Email == context.UserName).FirstOrDefault();
                    // var userAccount = con.UserAccounts.ToList();
                    // var user=userAccount.Find(u => u.Email.Trim() == context.UserName);

                    if (user == null)
                    {
                        context.SetError("invalid_grant", "The user name is incorrect.");
                        return;
                    }
                    else
                    {
                        if (!BCrypt.Net.BCrypt.Verify(context.Password, user.Password))
                        {
                            context.SetError("invalid_grant", "The Password is incorrect.");
                            return;
                        }
                    }
                }

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

                identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
                identity.AddClaim(new Claim("sub", context.UserName));

                var props = new AuthenticationProperties(new Dictionary <string, string>
                {
                    {
                        "as:client_id", (context.ClientId == null) ?  context.UserName : context.ClientId
                    },
                    {
                        "userName", context.UserName
                    }
                });

                var db         = new hlaplusEntities();
                var activeuser = db.UserAccounts.Where(u => u.Email == context.UserName).FirstOrDefault();
                //activeuser.LastActivityDate = DateTimeOffset.Now;
                //activeuser.LastLoginDate = DateTimeOffset.Now;
                //activeuser.IsOnline = true;
                db.SaveChanges();
                var ticket = new AuthenticationTicket(identity, props);
                context.Validated(ticket);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }