Attempting to decrypt a FormsAuthentication ticket cannot always verify data

I am using the new webapi.

Now I do not know if I am doing this correctly, but I am trying to configure the api to return the authentication cookie to the HttpResponseMessages header to use it in another mvc application.

I am using FormsAuthenticationTicket as I think I need to use e.g.

  public HttpResponseMessage Get(LoginModel model)
    {
        if (model.UserName == "bob")
        {
            //  if (Membership.ValidateUser(model.UserName, model.Password))
            // {
            var msg = new HttpResponseMessage(HttpStatusCode.OK);
            var expires = DateTime.Now.AddMinutes(30);
            var auth = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, expires,
                                                     model.RememberMe,"password",
                                                     FormsAuthentication.FormsCookiePath);
            var cookie = new HttpCookie("user");
            cookie.Value = FormsAuthentication.Encrypt(auth);
            cookie.Domain = "localhost";
            cookie.Expires = expires;
            msg.Headers.Add("result",cookie.Value);
            return msg;
            //   }
        }
        return new HttpResponseMessage(HttpStatusCode.Forbidden);
        //else
        //{
        //    return "The user name or password provided is incorrect.";
        //}
    }

now in my mvc application login controller I call the service and get the data value from the header set in the api controller.

   string data = response.Headers["result"].ToString();
   FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(data);

Every time I try to run FormsAuthentication.Decrypt, I get an error all the time

Failed to verify data.

I assume that due to the fact that the api encrypts the data, it uses some kind of key that the site does not know about. I'm right?

Can anyone help?

+5
1

, - , api , , , - . ?

FormsAuthentication.Encrypt Decrypt . , - -API ASP.NET MVC.

, , OAuth 2.0 -API.

+8

All Articles