Making user login persistant with ASP .Net Membership

后端 未结 3 1029
天命终不由人
天命终不由人 2020-12-08 16:29

I have a website that is built in ASP.NET 3.5 & SQL Server 2005, using the sql membership provider, and presumably forms authentication.

Since security needs on

3条回答
  •  抹茶落季
    2020-12-08 16:34

    I think you'd be better-off using the FormsAuthentication.SetAuthCookie method rather than writing a lot of code yourself.

    I believe your membership provider settings in the web.config may be conflicting with the settings you're providing in code, plus you're not providing a cookie name.

    Try the following:

    if (Membership.ValidateUser(userName, password))
    {
        FormsAuthentication.SetAuthCookie(userName, true); //Creates a cookie named "XXXAuth" - see settings in web.config below
    }
    

    In conjunction with the following settings in web.config:

    
        
    
    
    
    
        
            
            
        
    
    

    Simply change the "timeout" value in the authentication block to be a longer value if you really want to create an indefinite log-in period. I believe 432000 = 5 days.

    If you want your users to be able to explicitly log-out, simply call the following method in response to a button click (or whatever):

    FormsAuthentication.SignOut();
    

    Hope this helps.

提交回复
热议问题