使用ASP.NET MVC Web API中的userdata创建FormsAuthentication

时间:2013-11-21 15:14:46

标签: c# asp.net-mvc-4 asp.net-web-api forms-authentication

我正在尝试在插入userdata的Web API MVC 4中创建FormsAuthentication。我知道如何在客户端进行。

UserData userData = new UserData()
{
    SiteID = result.UserLite.SiteID,
    UserID = result.UserLite.ID,
    UserName = result.UserLite.UserName,
    SiteName = result.UserLite.SiteName,
    TokenValue = result.BaseSecurityToken.Value,
    FirstName = result.UserLite.FirstName,
    LastName = result.UserLite.LastName,
    Email = result.UserLite.Email,
    LanguageID = result.UserLite.LanguageID
};

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
         1,
         model.UserName,
         DateTime.Now,
         DateTime.Now.Add(FormsAuthentication.Timeout),
         false,
         Serialize(userData));

string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);

在我的Web API中,我没有对象Response。

FormsAuthentication.SetAuthCookie(model.UserName, false);

但它没有给我添加用户数据的可能性。

1 个答案:

答案 0 :(得分:0)

HttpContext.Current.Response

RequestResponse个对象是ASP.NET的核心,它们始终以某种方式可访问。