views:

189

answers:

2

In my web.config I have this:

<system.web>
    <authentication mode="Forms">
  <forms loginUrl="Login.aspx" protection="All" path="/" timeout="30"/>
    </authentication>
    <sessionState timeout="20" />
</system.web>

<location path="admin">
    <system.web>
        <authorization>
             <deny users="*"/>
             <allow users="admin"/>
        </authorization>
    </system.web>
</location>

I have two problems:

  1. In my admin path I want only the admin user to have access but I can't find a way to do this. How can I make only the admin user have access?

  2. The user always gets logged out even if I try to use cookies so he shouldn't be logged out. In my login.aspx I have the folloing code when the user is valid:

    FormsAuthentication.RedirectFromLoginPage(user, CheckBoxPersistCookie.Checked);
    

How can I make the user to stay logged in?

+1  A: 

try putting the <allow> line over the <deny> line.

<system.web>
    <authentication mode="Forms">
                <forms loginUrl="Login.aspx" protection="All" path="/" timeout="30"/>
    </authentication>
    <sessionState timeout="20" />
</system.web>

<location path="admin">
    <system.web>
        <authorization>
             <allow users="admin"/>
             <deny users="*"/>
        </authorization>
    </system.web>
</location>
Nick Spiers
Ah it was that easy, but I just thought it would be more logic that you first deny all then overwrite this by adding the admin after? :-)
Martin
In ASP.NET, authorizations rules are placed in order of priority.
richeym
Yeah, that one had me fooled for a little while one time too. .NET sees the deny * and kicks everyone before getting to the next line.
Nick Spiers
A: 

As I understand you have 30 mins timeout in your authentication cookie and 20 minutes in your session cookie. It seems that as session will expire in 20 minutes then it will be impossible to use authentication cookie too.
It is a little tricky if you want to leave user logged in. I know that it is possible to implement it using javascript and invisible iframe. You need to reload iframe every 5 minutes for example. Your session will be live and local cookies updated.

Danil
But if I want the user to be logged in the next time he opens the website? If he login today and then closes without logging out. Then if he open up the website from the same computer tomorrow I still want him to be logged in. thats why I tried to use cookies, but how do I implement it?
Martin
Hmm, In this case you need to implement some autologin code. But I'm wondering how secure is it.
Danil
I mean that cookies are not secure. You can save some hash in cookie and then try to autologin using this hash if your client code detects that auth cookie exist and user isn't loged in.
Danil
I thought the auto-login already was built-in in the asp.net authentication? Isn't it?
Martin