8

我们有一个习惯MembershipProviderASP.NET现在有 2 种可能的情况可以验证用户:

  1. login.aspx用户通过输入用户名/密码通过页面登录。我使用了登录控件并将其与MyMembershipProvider. 这工作得很好。

  2. 身份验证令牌通过不同网站的查询字符串中的某个 URL 传递。为此,我有一个重载 in MembershipProvider.Validate(string authenticationToken),实际上是在验证用户。在这种情况下,我们不能使用Login 控件。现在如何在不实际使用登录控件MembershipProvider的情况下使用它来验证用户?我尝试手动拨打电话,但这并没有让用户登录。Validate

这是我正在使用的代码片段

if (!string.IsNullOrEmpty(Request.QueryString["authenticationToken"])) {
    string ticket = Request.QueryString["authenticationToken"];
    MyMembershipProvider provider = Membership.Provider as MyMembershipProvider;
    if (provider != null) {
        if (provider.ValidateUser(ticket))
            // Login Success
        else
            // Login Fail
    }
}
4

3 回答 3

13

验证成功后,您需要登录用户,通过调用 FormsAuthentication.Authenticate:http: //msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.authenticate.aspx

编辑:它是 FormsAuthentication.SetAuthCookie:http: //msdn.microsoft.com/en-us/library/twk5762b.aspx

此外,要将用户重定向回他想去的地方,请调用:FormsAuthentication.RedirectFromLoginPage:http: //msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.redirectfromloginpage.aspx

链接文本

于 2008-09-03T13:29:53.367 回答
4

FormsAuthenticationTicket如果验证成功,您可以设置自己的。

像这样的东西;

if (provider != null) {
    if (provider.ValidateUser(ticket)) {
        // Login Success
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
            1, //version
            someUserName, //name
            DateTime.Now, //issue date
            DateTime.Now.AddMinutes(lengthOfSession), //expiration
            false, // persistence of login
            FormsAuthentication.FormsCookiePath
        );

        //encrypt the ticket
        string hash = FormsAuthentication.Encrypt(authTicket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

        Response.Cookies.Add(cookie);
        Response.Redirect(url where you want the user to land);
    } else {
        // Login Fail  
    }   
}
于 2008-09-03T17:29:03.293 回答
1

在将身份验证信息直接存储为 cookie 的情况下,您是对的。但是使用强大的散列函数(例如 MD5 + SHA1)既好又安全。顺便说一句,如果您使用会话(这也只是一个哈希 cookie),您可以将身份验证信息附加到它。

于 2011-04-18T05:30:01.547 回答