Get windows identity before authentication ASP.net - asp.net

Dear stackoverflow community,
Is there any way to retrieve windows username/identity even before authentication?
To make you all understand the situation, I am currently migrating an old ASP.net application. Previous code works no problem but due to recent code migration, old code won't work anymore. I want this application to be a single sign-on instead of the user need to key in their username and password WHILE using Forms authentication method.
It involves:
Index.aspx
HomePage.aspx
Login.aspx
Global.asax
The application flow:
Enter global.asax and check whether isAuthenticated is true or
false, if false, enter Index.aspx, if true, extract all the user
accessright into .isInroles
If not authenticated, get the windows username, check the user
exist in the application, and
FormsAuthentication.RedirectFromLoginPage(username, False/True)
and it will redirect to global.asax and redirect to Homepage. (This
is where single sign-on should be. And if there's no identity it
will return to Login.aspx page.
User provide username and password and authenticate like normal.
(This one works)
The thing is, when you are using Forms Authentication, the identity will return blank until the user provide one. For example HttpContext.Current.User.Identity.Name and other few more. Is there any way to get the windows identity before authentication?
If I change the web.config to windows authentication, it works perfectly fine but it require me to change the whole structure of the system because the way the system gave access right is very outdated using cookies .IsInRoles and my team won't agree because of time consuming process. Sorry I can't post the code snippet due to data confidentiality...
Any logical/pseudocode advice would be great.

The answer to this question, I restructured back the whole program authentication by using Windows authentication, and removed the current global.asax and replace with the new one since the previous global.asax was corrupted that if I comment the whole code in the global.asax it enters. That solves my problem.

Related

Avoiding authentication required popup while using integrated windows authentication and accessing application from internet

I need to implement something like Single Sign On for a application that is being used on both Intranet and Internet.
Now , the application uses its own table for storing User information and has more users than that present in the AD for the Company. Example contract workers/3rd party vendors etc and hence many users who don't belong to the Active directory of domain are listed in the User Table.
The Application is a bit old and currently it uses Form to authenticate the users.
But strangely authentication mode in the web.config file has the following entry for authentication. <authentication mode="None" />
I changed the authentication mode to Windows in web.config and in IIS 6 selected integrated Windows authentication and unchecked the anonymous access.
Now I have following two scenarios.
#1 Intranet
User logs in to the system using the System Credential which is stored in a AD
Now if user hits the link for the web application he should be logged in.
I have implemented this part by using Page.User.Identity.name in the Page load of login.aspx to check if the user exists in the DB.
#2 Internet
If I check it from a external network the browser prompts me for credential.
The requirement is that the user should not be prompted for credential instead should be shown the current Login page
I googled and ended up on stackoverflow every time. Sadly the solutions did not work out for me.
I stumbled upon this post by Scott Enabling Windows Authentication within an Intranet ASP.NET Web application and if you check the comments Scott refers to use of solution by commenter ripster in case application is accessed from internet as well as intranet. Though it didn't work out for me or may be I didn't do it properly.
It seems you require a mix of Windows and Forms authentication : The requirement is that the user should not be prompted for credential instead should be shown the current Login page
Thought to share this . May be it can help you as mostly I have seen people to like the second below mentioned solution a lot. ( Atleast when they read it.)
If you're in classic mode - you can have both Windows and Forms authentication. An alert will pop up
Challenge-based and login redirect-based authentication cannot be used
simultaneously
you can however ignore this warning. CarlosAg says that:
we decided to leave it there was because it is still behavior that many user
scenarios would be consider incorrect, since most of the time forms
authentication uses anonymous authentication and not windows.
Read here.
Now when you want to use integrated mode, This stack question : iis7 Challenge-based and login redirect-based authentication cannot be used simultaneously leads to this famous link: http://mvolo.com/iis-70-twolevel-authentication-with-forms-authentication-and-windows-authentication/, which allows to change the authentication way for a page.
Another way you can manage this when using windows authentication is to manage usernames using code:
string user = Request.ServerVariables["LOGON_USER"];
Refer this link: http://beensoft.blogspot.in/2008/06/mixing-forms-and-windows-authentication.html , which gives a different way of mixing Forms and Windows authentication.

Setup Windows Authentication in ASP .NET 3.5

Here's what I want to do (which I've done before but I'm clearly doing something that is not obvious to me...):
ASP .NET 3.5 Intranet Application
Want to have Windows Authentication against AD setup on the website
When the user requests the page, fetch the user's username with the following code:
System.Web.HttpContext.Current.User.Identity.Name
I have IIS 7.5 and have setup the web application, disabled anonymous access and enabled Windows Authentication.
When the page is requested, the prompt for username / password is entered.
I entered the details but the prompt keeps coming up and eventually comes back with 401.
What on earth have I missed?
It seems to be a broblem with the Authorization (dond confuse with Authentication they completely diffrent), becuse the Authentication has been passed successfully after you've written the right user name and password, please try to set the appropriate privilegs to the user you try to login with, then try again. You can do that by set ting the right roles in the Authorization tab in the Security/Application tool of ASP.NET or by defining it manually by creating the right class... You'd better also chack the IIS policy if you deal with production code on IIS. If this is not working please post the Authentication code you've written.
Figured out what was wrong. Was nothing to do with Authorization at all but I stupidly had written code a few days back throwing a 401 error when a certain condition was not met. Removing that proved that the setup done worked perfectly :)

Is Forms Authentication totally necessary?

I'm writing an internal web application right now (with ASP.Net Web Forms), and it presents an odd problem. I have to be able to impersonate the currently logged in windows user, and execute a command based on their Windows Authentication to log in.. AND ... if they don't have Windows Authentication set up in the application I have to use to log them in, I have to be able to accept a user name and password. I also have to write the application in .Net 4.0, and secure it as much as possible. I got this to work by NOT utilizing Windows Authentication or Forms Authentication in the web.config, and instead setting session variables to guard against user accessing pages in the web app other that the log in. I did this by creating an oddly name session variable with a value based on their user name (windows auth or not), and then a secret session variable. The secret variable is in the web.config as a 256bit encrypted string, in which I decrypt, and set as the session secret. In order for the page to load, the first session variable can't be blank, and the second variable has to equal the decrypted key value... if the variables don't pass inspection, it redirects them to the login page. I set this up on every page, generic handler, and webservice method in the web app. I make the session timeout after a few minutes of no activity, and on log out, I set all session variables to nothing, and expire all cookies. (I also disable all cache).
My question is... Does this offer comparable security to that of Forms authentication? I have always used Forms authentication, but can't use it here. If I did, the users would have to reconfigure settings in IIS and in he web.config to toggle login procedures (From my knowledge, you can't use both Forms authentication, and windows authentication to manage the security of your pages and other web resources). With the method described above, I can accomplish the best of both worlds, but am curious about the security of my methods. Is there anything else I can implement here to assure the utmost security other that using forms authentication? Is it possible to accomplish the same level of security of Forms authentication without using it?
Thank you for any insight in advance!
Does this offer comparable security to that of Forms authentication?
No
The first rule when it comes to security is don't reinvent the wheel unless you absolutely have to. Any home baked solution you come up with has the potential to be as secure as a provided one like Windows or Forms Authentication. The problem is that home-grown solutions rarely reach that potential. They may test okay, but subtle bugs can remain. You don't want to find out a year later that you were hacked six months ago. Existing solutions have already been tested and used in millions of applications, whereas yours will be used in one application and tested by a handful of people at most.
A quick search suggests that it is possible to implement both Windows and Forms Authentication in the same application, so I'd pursue it further.
Mixing Forms and Windows Security in ASP.NET

ASP.NET VB.NET Remote Login Portal Setup

Technology
ASP.NET, VB.NET 2.0 (soon to be 4.0)
Overview
I'm writing a Login / Authentication Portal, so that a web application can use it to login a user, and then they can use the application using their credentials.
The login portal will be a separate application, and initially only available via an extranet as well as intranet for certain applications; but future apps will need to authenticate via the web (happy to implement as a separate instance). I basically want other individual applications to be able to authenticate users via this portal.
So that...
A user goes to an application's web url (i.e. www.application.com / http://apps/application - intranet) and clicks "login".
User's browser is redirected to the portal application, with a query
string
www.loginportal.com/login.aspx?url=www.application.com/login.aspx
(or other page).
User fills in their credentials (username, password), and clicks
"login" button.
Browser redirects back to url i.e. www.applications.com/default.aspx or login.aspx and is authenticated and logged in; and can use app.
Completed
I have the authentication itself sorted, and will implement as a class library in the local applications via a dll.
Need
So I basically need to know, how to:-
1. Post data to the portal url (could be different domain).
2. Redirect browser with post.
3. Make sure that the authentication is secure, and not easily hackable (I know how to use urlencode and htmlencode etc) - just not sure about implications of posting data across domains.
Any help greatly appreciated...
Cheers,
Duncan.
Seriously tough stuff, here. If it were me, I'd lean heavily on Windows Identity Foundation. I believe it can support this scenario (haven't actually done it; someone else at my company is developing against it).
OK, so this is the solution I ended up using:
In the original application (the one that needs the authentication; step 1 above) I redirect the user to my login portal, and include the original url as a get parameter.
The user then types in their details, username and password.
Next, the server-side code authenticates them, and redirects to a new page, where I send back to the page an html form which includes the request datetime (for security) along with a encrypted string (including the datetime of the request) of the data I want sent back to the original form.
I also add a JavaScript post method which sends the data to the original url as a form post. Because I'm using the same class library at both ends, I can encrypt and decrypt the data using the same method and the original requesting application has all the user data, including the ability to check the datetime of the request (I allow a set amount of time between the authentication and the picking up by the original app, making sure these are within say 5 minutes.
And job done.
If anyone wants the code, I can provide it, just don't have it with me at the moment, if I remember I'll post it.
Not the most elegant solution, but it works, and it's secure, so I'm happy. :).

Using Windows Integrated Auth & Anonymous after jakarta redirect on IIS7

I have an application we bought that I need to integrate, and it uses jakarta connection to get to the application from IIS.
So, the basic operation is:
user goes to the url
Gets redirected to the application
SSO is enabled, so redirected back
to IIS for fetching of domain
credentials
Back to application
If username is blank show login
page, else let user in.
This is a simplification of all the steps, but the basic idea is here.
My difficulty is that I need both Windows Integrated Auth and anonymous on, as some users won't have credentials, and need to be prompted for a username/password.
I have looked at: IIS Windows Authentication before Anonymous already, but the user doesn't get to click on a link to decide. The application goes back to IIS looking for login.aspx and from there I want to either get their domain credentials or pass back to the application empty strings to signify that there are no credentials.
It seems this isn't going to be possible though as if anonymous is on it doesn't make the 401 request so the credentials aren't passed.
If I can't get this to work with just using an ASP.NET page, could it be done using an ISAPI filter, or a module?
UPDATE:
I found a possible solution, but I need to figure out how to get it to work, as my login page is on the JBoss server.
http://mvolo.com/blogs/serverside/archive/2008/02/11/IIS-7.0-Two_2D00_Level-Authentication-with-Forms-Authentication-and-Windows-Authentication.aspx
Keep the Authentication in the IIS as the Anonymous.
When loading the home page check the ACTIVE DIRECTORY for the current logged in USERNAME if exist
provide the extra functionality to current user or else with fewer options.
Refer --> Active Directory Cheking

Resources