title: WCF, MVC4 and authentication : customizing error
body: <p>I'm trying to custom the "default authentication" that is provided by Visal Studio project.
  I've a database with some users inside that is linked to my project. What I want to do : ensure that login/password couple is in the database and so connect user.</p>
  
  <p>Just to give you ideas of what have been done :
  AccountController : </p>
  
  <pre><code> [HttpPost]
   [AllowAnonymous]
   [ValidateAntiForgeryToken]
   public ActionResult Login(LoginModel model, string returnUrl)
   {
      try
      {
          var context = new MyService.MyEntities2(new Uri("http://localhost:12345/MyWS.svc/"));
          var usr = from user in context.PERSON
                    where user.LOGIN == model.UserName &amp;&amp; user.PASSWORD == model.Password
                    select user;
          List&lt;MyWebPortal.MyService.PERSON&gt; lpers = usr.ToList();
          int nbRes = lpers.Count();
          if (ModelState.IsValid &amp;&amp; nbRes ==1)
          {
              return RedirectToLocal(returnUrl);
          }
      }
      catch (DataServiceQueryException ex)
      {
          ModelState.AddModelError("", "Erreur : "+ex.ToString());
      }
      ModelState.AddModelError("", "Authentication error");
      return View(model);
  }
  </code></pre>
  
  <p>LoginModel :</p>
  
  <pre><code>public class LoginModel
      {
          [Required]
          [Display(Name = "User name")]
          public string UserName { get; set; }
  
          [Required]
          [DataType(DataType.Password)]
          [Display(Name = "Password")]
          public string Password { get; set; }
      }
  </code></pre>
  
  <p>Login form :</p>
  
  <pre><code>@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true)
  
  &lt;fieldset&gt;
      &lt;ol&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.UserName)
              @Html.TextBoxFor(m =&gt; m.UserName)
              @Html.ValidationMessageFor(m =&gt; m.UserName)
          &lt;/li&gt;
          &lt;li&gt;
              @Html.LabelFor(m =&gt; m.Password)
              @Html.PasswordFor(m =&gt; m.Password)
              @Html.ValidationMessageFor(m =&gt; m.Password)
          &lt;/li&gt;
      &lt;/ol&gt;
      &lt;input type="submit" value="Login" /&gt;
  </code></pre>
  
  <p>But i've got the following error :</p>
  
  <pre><code>System.Data.Services.Client.DataServiceQueryException: Une erreur s'est produite lors du traitement de cette requête. ---&gt; System.Data.Services.Client.DataServiceClientException: &lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;&lt;error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"&gt;&lt;code&gt;&lt;/code&gt;&lt;message xml:lang="fr-FR"&gt;Une erreur s'est produite lors du traitement de cette requête.&lt;/message&gt;&lt;/error&gt; 
  à System.Data.Services.Client.QueryResult.ExecuteQuery(DataServiceContext context) 
  à System.Data.Services.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents) 
  --- Fin de la trace de la pile d'exception interne --- 
  à System.Data.Services.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents) 
  à System.Data.Services.Client.DataServiceQuery`1.Execute() 
  à System.Data.Services.Client.DataServiceQuery`1.GetEnumerator() 
  à System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
  à System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
  à MyApp.Controllers.AccountController.Login(LoginModel model, String returnUrl) dans d:\....\Documents\Visual Studio 2012\Projects\MyProg\MyWS\Controllers\AccountController.cs:ligne 45
  </code></pre>
  
  <p>Where ligne 45 is </p>
  
  <pre><code>List&lt;MyWebPortal.MyService.PERSON&gt; lpers = usr.ToList();
  </code></pre>
  
  <p>Did I do something wrong with the request ? Thanks !</p>