示例#1
0
 public JsonResult AuthenticateUser(string userName, string password, string deviceId)
 {
     try
     {
         var authenticated = Membership.ValidateUser(userName, password);
         if (authenticated)
         {
             var sessionGuid = Guid.NewGuid();
             var session     = new Session()
             {
                 DeviceId    = deviceId,
                 SessionGuid = sessionGuid,
                 UserName    = userName,
                 OpenedOn    = DateTime.Now,
                 IsValid     = true,
                 ClosedOn    = DateTime.Now.AddYears(1)
             };
             DbHelpers.AddSession(session);
             var response = new AuthenticationToken
             {
                 IsAuthenticated = authenticated,
                 Message         = "Success",
                 Role            = Roles.GetRolesForUser(userName).Equals("Administrator")
                     ? SmartVitals.Services.Enums.Roles.Administrator.ToString()
                     : SmartVitals.Services.Enums.Roles.User.ToString(),
                 Profile   = UserInfo.GetFromProfile(WebProfile.GetProfile(userName, authenticated)),
                 SessionId = authenticated ? sessionGuid : new Guid()
             };
             return(Json(
                        new JsonResponse <AuthenticationToken> {
                 Response = response, Success = true
             },
                        JsonRequestBehavior.AllowGet
                        ));
         }
         return(Json(
                    new JsonResponse <AuthenticationToken> {
             Response = null, Success = false, Message = "Invalid username or password"
         },
                    JsonRequestBehavior.AllowGet
                    ));
     }
     catch (Exception)
     {
         return(Json(
                    new JsonResponse <AuthenticationToken> {
             Response = null, Success = false, Message = "An error occured while processing your request."
         },
                    JsonRequestBehavior.AllowGet
                    ));
     }
 }