I want use fullcalendar in my MVC project. My project include EWS API Exchange
. I want get appointment in exchange. Also I am using ad authentication: Configure ASP.NET MVC for authentication against AD
I created a user model:
public class AccountModels
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
Then AccountController:
public ActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(AccountModels model, string returnURL)
{
if (!this.ModelState.IsValid)
{
return this.View(model);
}
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (this.Url.IsLocalUrl(returnURL) && returnURL.Length > 1 && returnURL.StartsWith("/")
&& !returnURL.StartsWith("//") && !returnURL.StartsWith("/\\"))
{
return this.Redirect(returnURL);
}
return this.RedirectToAction("Index", "Home", model);
}
this.ModelState.AddModelError(string.Empty, "The username or password is incorrect");
return this.View(model);
}
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return this.RedirectToAction("Account", "Login");
}
}
The model creation is fine and then I redirect to Home
Controller on Index
method. In Index method, I create my ExchnageServer, but after Index
is done, my model object becomes null.
This Home Controller
public ActionResult Index(AccountModels model)
{
ServerExchangeCore server = new ServerExchangeCore(model);
return View(server);
}
// [Authorize]
public JsonResult GetAppointments(ServerExchangeCore serverExchange)
{
ServerExchangeCore server = serverExchange;
server.SetServer();
JsonResult result = new JsonResult();
List<Appointment> dataList = server.GetAppointments();
result = Json(dataList, JsonRequestBehavior.AllowGet);
return result;
}
And this my ServerExchnageCore
class
public class ServerExchangeCore
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016);
AccountModels _model = new AccountModels();
public ServerExchangeCore()
{ }
public ServerExchangeCore(AccountModels model)
{
_model = model;
}
public void SetServer()
{
string email = GetEmail()
service.Credentials = new WebCredentials(_model.UserName, _model.Password);
service.AutodiscoverUrl(email, RedirectionUrlValidationCallback);
}
public List<Appointment> GetAppointments()
{
DateTime startDate = DateTime.Now;
string sssssss = service.Url.ToString();
DateTime endDate = startDate.AddDays(30);
CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
CalendarView calendarView = new CalendarView(startDate, endDate);
calendarView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
FindItemsResults<Appointment> appointments = calendar.FindAppointments(calendarView);
List<Appointment> lst = new List<Appointment>();
foreach (Appointment aa in appointments)
{
lst.Add(aa);
}
return lst;
}
private string GetEmail()
{
if (HttpContext.Current.User.Identity.Name != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
MembershipUser user = Membership.GetUser();
if (user != null)
userEmail = user.Email;
}
userEmail = Membership.GetUser().Email;
return userEmail;
}
How do I fix my Home
Controller?