I would like to create a site using windows authentication because this site will be acting as an intranet site and it would be more convenient for the different user groups if authentication is handled by logging on to windows. I have a few questions about
implementing windows authentication.
What is the razor syntax to check the user group that the user is logged in as?
Will this affect other sites hosted on the same server?
Does anyone have experience in using windows authentication?
if(Roles.IsUserInRole(WebSecurity.CurrentUserName, @"MyDomain\Students")){
//they are a student
}
if(Roles.IsUserInRole(WebSecurity.CurrentUserName, @"MyDomain\Administrators")){
//they are an administrator
}
//etc
Have I done something wrong because @WebSecurity.CurrentUserName doesn't render anything? I have set Windows Authentication to enabled in Visual Studio and my web.config file looks as follows
Member
445 Points
176 Posts
Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 05, 2013 05:17 AM|mhcodner|LINK
I would like to create a site using windows authentication because this site will be acting as an intranet site and it would be more convenient for the different user groups if authentication is handled by logging on to windows. I have a few questions about implementing windows authentication.
All-Star
193991 Points
28021 Posts
Moderator
Re: Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 05, 2013 08:46 AM|Mikesdotnetting|LINK
You can get the name of the currently logged in user from WebSecurity.CurrentUserName just as if you were using Forms Authentication.
The method of authentication that you choose is set on a site-by-site basis.
Here's an article that shows how to implement Windows Authentication in Web Pages: http://mikepope.com/blog/AddComment.aspx?blogid=2298
Member
445 Points
176 Posts
Re: Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 05, 2013 09:57 PM|mhcodner|LINK
Thank you but how can I check if the user is logged in as an administrator and how to check what user group they are logged in as?
The situation is that I would like different access for students, teachers and administrators and there are different user groups for each of these.
All-Star
193991 Points
28021 Posts
Moderator
Re: Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 06, 2013 01:54 AM|Mikesdotnetting|LINK
You enable roles in the web.config and specify the WindowsTokenRoleProvider as role provider. Here's a sample web.cofig:
Then you can use Roles:
Member
445 Points
176 Posts
Re: Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 06, 2013 02:58 AM|mhcodner|LINK
Thank you very much
Member
445 Points
176 Posts
Re: Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 06, 2013 05:37 AM|mhcodner|LINK
Have I done something wrong because @WebSecurity.CurrentUserName doesn't render anything? I have set Windows Authentication to enabled in Visual Studio and my web.config file looks as follows
All-Star
193991 Points
28021 Posts
Moderator
Re: Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 06, 2013 05:49 AM|Mikesdotnetting|LINK
Have you also set Anonymous Authentication to Disabled?
Member
445 Points
176 Posts
Re: Windows authentication with ASP.NET Web Pages/WebMatrix
Aug 06, 2013 05:51 AM|mhcodner|LINK
Yeah I read that somewhere else and tried it. It worked so thank you anyway.