I have problem with using my Custom Role Provider.
I`ve made class CustomRoleProvider : RoleProvider and method string [] GetRolesForUsers() is overrided:
public override string[] GetRolesForUser(string username)
{
string[] s = { "StandardAdmin" };
return s;
}
My web.config now looks like:
<roleManager enabled="true" defaultProvider="AccessRoleProvider" >
<providers>
<clear />
<add name="AccessRoleProvider" type="Formularz.Memberships.AccessRoleProvider" applicationName="/" />
</providers>
</roleManager>
and
<location path="Memberships.AdminsListPage.aspx">
<system.web>
<authorization>
<allow roles="SuperAdmin"/>
<deny users="*"/>
</authorization>
</system.web>
I have no idea why i can access AdminsListPage.aspx, so I hope you could give some advice for me.
Thank you in advance, Peter
Related
I'm trying to develop an ASP.NET MVC application which has few views. I have the following code for controller to restrict access for users:
Controller:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (!string.IsNullOrEmpty(model.ReturnUrl))
return Redirect(model.ReturnUrl);
return RedirectToAction("Edit", "Home");
}
ModelState.AddModelError("Password", "The user name or password provided is incorrect");
}
// if we got this far, something failed, redisplay form
return View(model);
}
In Web.Config:
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Home/Login" timeout="10" />
</authentication>
<authorization>
<allow users="*" />
</authorization>
<membership defaultProvider="LabourTimeProvider">
<providers>
<clear />
<add name="LabourTimeProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="LabourTime" attributeMapUsername="sAMAccountName" />
</providers>
</membership>
<roleManager defaultProvider="CustomRoleProvider" >
<providers>
<clear />
<add name="CustomRoleProvider" type="LabourTime.CustomRoleProvider" />
</providers>
</roleManager>
</system.web>
<location path="Home/Login">
<system.web>
<authorization>
<allow roles="mydomain\mygroup" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="Home/Edit">
<system.web>
<authorization>
<allow roles="mydomain\mygroup" />
<deny users="*" />
</authorization>
</system.web>
</location>
In location path, if I use something like,
allow users = "my-user", it is working fine. Only the user is having access.
However, I would like a group of users in the mygroup to access the page. I don't know how to achieve this. I tried this code by doing some research but it didn't work. What to do I do in order to get access for the whole group?
When I try to login using an ID from the group, it doesn't work. Any suggestion is appreciated. Thanks!
There are different ways of achieving access by groups. Since you already use attributes I would suggest using the following approach:
[Authorize(Roles="Administrators")]
public class AdminController : Controller
{
.....
}
When you wanna put logic within your code you can use a construction like this:
if (Roles.IsUserInRole(userName, "ConfirmedUser")
{
.....
}
In your example, it is clear you are talking about a domain joined users group (part of an Intranet). In general Group Policy Objects (GPO) and Security Groups (SGs) are created within in Active Directory (AD), and domain users are member of these SGs.
In other cases DB hosted on DB Server can also be linked to same SGs, and in some cases DBs are not linked to any AD SGs, but have a different login account for an added security.
Access to these SGs are managed by IT Support Specialists of a given organization.
<authorization>
<allow users="*" />
</authorization>
Having said that, upon using <allow users="*" /> within Web.config will only allow domain users whose domain accounts are member of their appropriate Security Groups (SGs), created for their organization with in Active Directory (AD). As long as the application being developed is deployed on Application server joined to same domain, the GPOs and SGs security automatically gets synchronized to users and computer accounts for that domain. Therefore, only users member of SGs are able to access the application within an Intranet.
Using Visual Studio 2015 Community, EF 6, ASP.NET MVC 5, Oracle, and using a database-first approach.
I'm trying to setup my application so that an AD and a list of users are the only ones who can access it. The list of users will not be in this AD group. This is for an internal application. In my web.config file I have the following
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
<providers>
<clear />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
Above each of my controllers I have the following:
[Authorize(Roles = #"domain\AD_Group, domain\user1,domain\user2,domain\user3")]
public class HomeController : Controller
I've tried different ways of handling this with:
[Authorize(Roles = #"domain\AD_Group" Users = #"domain\user1,domain\user2,domain\user3")]
And
[Authorize(Roles = #"domain\AD_Group")]
[Authorize(Users = #"domain\user1,domain\user2,domain\user3")]
But nothing seems to work. Either everyone loses access or only the AD has access. What am I doing wrong? Should I handle this in the web.config file rather than my current method? What would that look like? Thanks.
Just like the title states. Need the windows logon and domain info from within our asp.net page.
I tried
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
but it returns the IIS App Pool not the user name
Thanks
Try HttpContext.User, accessible simply as User from the code behind. It returns both the domain and username, but should be easy enough to trim for your needs. It's worked for me in the past. You can also use this to manage roles in your application, if you need to.
EDIT
Below are the relevant portions of my web.config. I also used aspnet_regsql.exe to setup the tables needed for the role manager in my database. I could then use User.Identity.Name and User.Identity.IsInRole
<connectionStrings>
<clear/>
<add name="SqlRoleManagerConnection"
connectionString="myConnectionString">
</add>
</connectionStrings>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
<roleManager enabled="true" defaultProvider="SqlRoleManager">
<providers>
<clear/>
<add name="SqlRoleManager"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="SqlRoleManagerConnection"
applicationName="myAppName" />
</providers>
</roleManager>
</system.web>
This is my code in VB.net
var strUser = System.Web.HttpContext.Current.User.Identity.Name
so C# must be in the lines of: (not tested)
string strUser = System.Web.HttpContext.Current.User.Identity.Name;
In the Web.Config file
<configuration>
<system.web>
<authentication mode="Windows"/>
<identity impersonate="true" />
</system.web>
</configuration>
I have an MVC 4 application which is open to all users, no login needed. There is one controller only which I need to apply Windows Authentication to via a Web.Config, like this:
<authentication mode="Windows" />
<authorization>
<allow users="domain\jsmith" />
<deny users="*" />
</authorization>
The controller would MySite.Com/MyApp/MyAdminReportController
If this is possible, how?
I think you just need Windows auth and specify paths which are only need authorization. If you don't need Forms auth as well it looks like this:
<configuration>
...
<system.web>
......
<authentication mode="Windows">
</authentication>
</system.web>
<location path="MyAdminReport">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
</configuration>
This is web config approach, other options is adding [Authorize] attribute to your controllers (even not hole controller you can add this attr for only specific actions too).
[Authorize]
public class MyAdminReportController : Controller
{
//[Authorize]
public ActionResult PrivatePage()
{
return View();
}
}
I am having some trouble when I use ASP .Net 4's URL Routing feature while Authorization rules configured.
Global.asax
void Application_Start(object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes) {
routes.MapPageRoute("dashboard", "", "~/Restricted/Default.aspx", true);
routes.MapPageRoute("register", "register", "~/Register.aspx", true);
routes.MapPageRoute("login", "login", "~/Login.aspx", true);
}
{Root}\Web.Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms name="DevAuth"
loginUrl="/login/"
protection="All"
path="/"
timeout="15"
requireSSL="false"
slidingExpiration="true"
cookieless="AutoDetect" />
</authentication>
</system.web>
<system.webServer>
<security>
<authentication>
<basicAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
</configuration>
{Root}\Restricted\Web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<security>
<authorization>
<remove users="*" roles="" verbs="" />
<add accessType="Allow" roles="Developer" />
<add accessType="Deny" users="*" />
</authorization>
</security>
</system.webServer>
</configuration>
The problem I am facing is:
When I try to visit http://localhost/ -- because of my dashboard rule in Global.asax, instead of being redirected to http://localhost/login/?ReturnUrl=%2f, I am actually getting the content of http://localhost/Restricted/Default.aspx page.
when I try to visit http://localhost/Restricted/ -- I do get redirected to http://localhost/login/?ReturnUrl=%2fRestricted -- which is a good sign!
Any idea about what's going on?
EDIT 1
The following change in the config file gives me Access is denied.
{Root}\Web.Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<authentication mode="Forms">
<forms name="DevAuth"
loginUrl="/login/"
protection="All"
path="/"
timeout="15"
requireSSL="false"
slidingExpiration="true"
cookieless="AutoDetect" />
</authentication>
</system.web>
<system.webServer>
<security>
<authentication>
<basicAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
<location path="login">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="register">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>
Hummmm I think it comes around this :
<location path="">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
The problem I see here comes from this path="", because this information says to the UserAgent [Browser, like IE or FF or Chrome] to block this address : http://localhost:xxxxx
which in fact, points out to your default route : ~/Restricted/Default.aspx
You are denying access to this page by default to all users. Hopes it gives you a hint on how to do this.
You are actually not using URL Rewriting; you are using Routing. There's a significant difference between the two that is likely causing your trouble: With Routing, the URL you are requesting is never changed. So the authorization system is still doing its work based on the URLs typed in the address bar... it knows nothing at all about what the routing engine is doing after.
That explains your initial behavior perfectly; Requesting the root/default (empty string route value) is permitted according to your initial auth rules. The fact that Routing is causing ~/Restricted/Default.aspx to be the content loaded is immaterial - that is, it is ignored. Likewise, directly requesting /Restricted/ would, then, trigger the auth mechanism.
Routing and file/location-based Authorization are actually very tricky to use together, for just this reason.
On the other hand, if you were using Rewriting (where the actual URL being requested is changed), things would work as you expect them to.
As Andrew Barber writes your authentication rules will not come into play when you use Routing in this way.
You can read more about routing and authentication/authorization here: http://blogs.msdn.com/b/mikeormond/archive/2008/06/21/asp-net-routing-and-authorization.aspx..