我在 web 应用程序 4 中使用自定义成员资格提供程序,并在 web.config 中使用此代码:
<httpCookies httpOnlyCookies="true" />
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" timeout="2880" />
</authentication>
会员资格和登录正在工作,但请记住我不工作。
编辑:
并将此代码用于登录用户:
public static void SetupFormsAuthTicket(string userName, bool persistanceFlag)
{
using (EntitiesConnection EF = new EntitiesConnection())
{
var obj = (from m in EF.Memberships.ToList()
where m.Username == userName
select m).FirstOrDefault();
var userId = obj.UserId;
var userData = userId.ToString(CultureInfo.InvariantCulture);
var authTicket = new FormsAuthenticationTicket(1, //version
userName, // user name
DateTime.Now, //creation
DateTime.Now.AddMinutes(30), //Expiration
persistanceFlag, //Persistent
userData);
var encTicket = FormsAuthentication.Encrypt(authTicket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
if (authTicket.IsPersistent)
{
cookie.Expires = authTicket.Expiration;
}
HttpContext.Current.Response.Cookies.Add(cookie);
}
}