Vikram Singh Saini
asked on
MVC4 Window authentication without prompt
Hello Experts,
I am working on porting of Asp.Net WebForm to MVC4 application.
** The website and users who will access websites are in same domain.
In old website users were restricted by creating a separate folder with web.config allowing only users with Admin role as -
So to implement same in MVC on Admin controller, I marked controller as -
Q.1. After publishing on server, when I try to access respective controller, the browser show prompt asking for username and password.
* I know of that it is browser normal behaviour to show that. But if it is true, then why it is not showing same prompt for earlier website?
Q.2. I am looking for some sort of solution that allow me to following -
(i) Get window's logged-in username that is making request.
(ii) Check role of that user matching with roles defined by me (for e.g. in web.config).
(iii) Authorize or allow user to access controller basis on result of step 2.
* I'm aware that I can get logged username by HttpContext.Current.User.I dentity.Na me. But it is returning me null if I am not using Window Authentication. And if I am using WA, then it will work. But again a prompt window comes.
I belive that I'm not only the one who is facing this problem or had faced same for MVC. There might be other experts who fixed this for their websites. So looking for practical solution or hints.
IN SHORT -
I want to allow access to specific controller for specific users with Admin role using their logged in credentials (without asking them for their credentials with prompt again).
I am working on porting of Asp.Net WebForm to MVC4 application.
** The website and users who will access websites are in same domain.
In old website users were restricted by creating a separate folder with web.config allowing only users with Admin role as -
<authorization>
<allow users="?" />
<allow roles="Administrators" />
</authorization>
So to implement same in MVC on Admin controller, I marked controller as -
[Authorize(Users="?")]
[Authorize(Roles = @"Administrators")]
public class AdminController : Controller
{
}
Questions :
Q.1. After publishing on server, when I try to access respective controller, the browser show prompt asking for username and password.
* I know of that it is browser normal behaviour to show that. But if it is true, then why it is not showing same prompt for earlier website?
Q.2. I am looking for some sort of solution that allow me to following -
(i) Get window's logged-in username that is making request.
(ii) Check role of that user matching with roles defined by me (for e.g. in web.config).
(iii) Authorize or allow user to access controller basis on result of step 2.
* I'm aware that I can get logged username by HttpContext.Current.User.I
I belive that I'm not only the one who is facing this problem or had faced same for MVC. There might be other experts who fixed this for their websites. So looking for practical solution or hints.
IN SHORT -
I want to allow access to specific controller for specific users with Admin role using their logged in credentials (without asking them for their credentials with prompt again).
in the website are you set for windows authentication or forms authentication? you also have to setup asp website authentication
ASKER
First of all thanks to both of experts for answering to neglected question.
@TheLearnedOne
Yes! You are right and the excerpt from link speaks truth.
@David Johnson
Yes! I had set for Windows authentication both by IIS and in web.config too.
To both experts
Is there any way that I can get logged in user name from request without using Window authentication?
@TheLearnedOne
Yes! You are right and the excerpt from link speaks truth.
@David Johnson
Yes! I had set for Windows authentication both by IIS and in web.config too.
To both experts
Is there any way that I can get logged in user name from request without using Window authentication?
If you have Windows Authentication working correctly, then the identity shouldn't be null. I believe that the problem is that the identity is null, so it is asking you to log in.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was able to fix the concerned problem by that solution.
Authenticating Users with Windows Authentication (C#)
http://www.asp.net/mvc/tutorials/older-versions/security/authenticating-users-with-windows-authentication-cs