Login Membership .NET - asp.net

Hi I was wondering what is the best way to force the user to login when arriving at a website, in .net. I have set up the Membership features and I was wondering what is the best way to ensure that no matter what address the user arrives at, they must first get authenticated before proceding to the page they requested. Any resources will be great, thanks.

Alter Web.config in application root to read:
<authentication mode="forms">
<forms loginUrl="Login.aspx" defaultUrl="/" />
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
Side note: Obviously, ASP.NET can only protect the requests that are handed down to ASP.NET engine. By default, it cannot enforce the security on static resources in IIS classic mode. In that case, to control access to static resources, they should be explicitly mapped to the ASP.NET ISAPI DLL in the IIS configuration.
More info about Authorization here: ASP.NET Authorization.

Forms Authentication explained
Here's a sample from a web.config
<forms loginUrl="Login.aspx"
protection="All"
timeout="30"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="default.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />

Related

Sharing windows authentication accross sites in IIS7/IIS7.5

I have a parent website (developed like 5 yrs before) say (sky) which I host in IIS7/IIS7.5 and configure it to use Form authentication and anonymous authentication. The web.config with entries as follows:
<authentication mode="Forms">
<forms loginUrl="Login.aspx" protection="All" timeout="480" name=".ASPXAUTH" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="~/Home" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
<location path="cloud">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Now I have a child website (developed 2 days ago) say (cloud) which I host under sky and configure it to use Windows authentication and anonymous authentication. Its own web.config has the following entries.
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<identity impersonate="false" />
When I access sky/, I end up on the login form, which is expected. I enter the username/pass and I can browse the parent website absolutely fine.
Unfortunately, when I access sky/cloud/, a username/password prompt box shows up, I enter the correct creds, but it does not authenticate and I cannot browse the child website. It repeatedly throws the creds box.
I switched the priority of authentication provider to do NTLM first over Negotiate on cloud, still no luck.
Why does the new site need to be under the parent site? Gotta blame branding here :(
Any idea's on getting this working would be appreciated.

HTTP Error 404.0 - Not Found Membership Provider Authorization Access

I'm using Membership Provider for create and manage users and roles in my site. I'm trying to restric access to a specific roles to the Account folder using web.config into this folder with this rule:
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*" />
</authorization>
</system.web>
I'm using Web Forms and forms authentication like show below:
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" timeout="2880" defaultUrl="~/" />
</authentication>
But when I try to access to page located into Account folder I'm getting:
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. Below a picture with the error:
How can I do for redirect to Login when the user try to access to unauthorized folder or url?
You are accessing the Login.aspx like /Account/Login/Default.aspx which doesn't exist.
Shouldn't that be ~/Account/Login.aspx?
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
How to debug?
Make sure you can access Login.aspx properly.
Then paste that URL inside form tag in web.config like above.

Asp.Net 4 Form Authentication issue

I've a strange issue with a .NET 4.0 website published on IIS6.
It implements the Forms Authentication, and in my Web.Config I've something like:
<authentication mode="Forms">
<forms loginUrl="~/login.aspx" timeout="2880" name="AUTH_TOKEN" />
</authentication>
<authorization>
<allow roles="__AUTHUSER__" />
<deny users="*" />
</authorization>
And I've the Default.aspx setted as default document in the website.
If I try to access the www.site/default.aspx it redirects me to the www.site/login.aspx?ReturnUrl=%2fDefault.aspx
And it is fine.
The problem is that if I try to access www.site/default.aspx?ReturnUrl= it gave me a 401.2 error page: not authorized.
It's happening that this is a rebuild of an intranet application and many users actually (for some reason) have the www.site/default.aspx?ReturnUrl= url saved in the bookmarks... so I need to get it work.
Any ideas?

common authentication logic in asp.net membership

I m newb in asp.net. I m working for an example with Membership class in ASP.NET. I have some pages in my project e.g. default.aspx, page1.aspx, page2.aspx. I have put login control in default.aspx. Now I want to check authentication of user when user want to access any page. Already I achieved this thing to put below snippest in Page_Load event in every Page. But want some suggestion so that i can remove this duplicate code from every page. and implemete some logic which can be applicable on every page. Do i need to use global.asax or any class which could be inherite in all page an check authentication?
if(!HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.redirect("default.aspx");
}
You can set up your default login page from the authentication element of the web.config file in the root of your asp.net application.
an example...
<authentication mode="Forms">
<forms loginUrl="Login.aspx"
protection="All"
timeout="30"
name=".ASPXAUTH"
path="/"
defaultUrl="default.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />
</authentication>
Controlling access to files and folders is done via the authorisation element.
an example...
<authorization>
<allow roles="Admin" />
<deny users="?" />
</authorization>
See the documentation for full details and more examples.
In .net, you can set this within the web.config using the authorization element.
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
This will deny all users, who are not authenticated, access to any other page apart from the Login page which you specify in the authentication element.
<system.web>
<authentication mode="Forms">
<forms loginUrl="Login.aspx"
protection="All"
timeout="30"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="default.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />
</authentication>
</system.web>
As taken from here
For the purpose of the authorization, Baldy is correct.
For any other code that you may not want to duplicate per page, you may want to place it in a masterpage to which all of your pages belong.
Masterpages:
http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

ASP.NET authorization, show browser login dialog

I have such section in my web.config to deny access of anonymous users to elmah.axd. Is it possible to make exception and add credentials for admin user to allow that user access protected web page? I would like to display browser authentication dialog and ask user for credentials before accessing elmah.axd url.
<location path="elmah.axd">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
Here is how to do it.
http://www.xoc.net/works/tips/forms-authentication.asp
By the way browser authentication forms are usually used in intranets with Windows authentication. And not forms authentication.
Yes:
Add this to <system.web>
<authentication mode="Forms">
<forms name=".ASPXAUTH"
loginUrl="Login.aspx"
protection="All"
timeout="30"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="Login.aspx"
cookieless="UseCookies"
enableCrossAppRedirects="false"/>
</authentication>
It will redirect anonymous users to Login.aspx

Resources