how to use Authorize with #inherits? - c#

c# with a blazor application
I'm using Microsoft Identity Web to get the scopes from my Azure AD. This is working and I can use
[Authorize(Policy = "GroupAll")]
[AuthorizeForScopes(Scopes = new[] {Constants.ScopeUserRead})]
This is working fine but now I want to create 2 pages with page 1 a different policy then page 2. And page 2 is using #inherits to reuse 99% of the code of page 1.
How do I do this? because the [authorize] code is before the
public partial class classname : ComponentBase
I hope my problem is clear. Sorry im still learning.
Maurice

Related

How to modify asp.net Identity UI for asp.net core WebAPI with angular

I started learning .net core a few days ago and as a start, I created a .netcore project with an inbuilt angular 8 templates.
It has a couple of pages built in angular, like counter and fetches data, etc, but all the Identity-related pages (login, registration, etc) are coming as a plain HTML from the backend. So "my main Concern is making some UI changes in that page".
I found out that the identity has been added to the class library and hence not visible in the backend code. and In order to make changes to it, I will need to first add it to the code by regenerating it through scaffolding. this will override the previous library code and can be modified as per requirement.
So I selected add new scaffolded item --> identity. and selected "override all files" in first attempt and "account\login" and "account\register" only in the second attempt, and applicationdbcontext for dbcontext. but unfortunately neither worked for me.
When I try to build the code I get this error
Error CS0246 The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)
C:\Users\MyUserName\source\repos\MyProjectName\obj\Debug\netcoreapp3.0\Razor\Pages\Shared\_Layout.cshtml.g.cs 448
I don't really know what .g.cs extension is, there is no import statement on that page and some weird code that I have never seen before. the page itself also doe's not shown any error/red marking.
I basically want to achieve two things here,
1) using .net core app completely as an API, I want to build UI for all the login related stuff in angular itself rather than getting as an HTML from the back-end.
2) adding a few more fields in the user login form. and since it is code first approach then ultimately making changes in users DBSet (which I don't know how to do in this case).
I have not added any code sample as it is a kind of straight forward problem/question. to reproduce the issue just create a project in .net core 3.0 and take an angular 8 templates. now try to make any UI related change on login or register page.
It's annoying that this piece which isn't necessary in the layout for a default project lives there.
Try to drop that import and that default app with scaffolded UI will work OK.
My intention was to add some styles for the Login page which is a bit boring.
What have I done?
+ created an ASP.NET Core web application with Angular and Authentication (Individual User Accounts) template (from Visual Studio 2019).
+ Add New Scaffolded Item -> Identity
+ Followed the steps from ScaffoldingReadme.txt file (some steps have already snippets in the correct place and does not require any additions)
+ Remove the line #inject IWebHostEnvironment Environment and Microsoft.AspNetCore.Hosting from the _Layout.cshtml page;
+ READY to see the RESULT :)
just check the guide from https://devblogs.microsoft.com/aspnet/aspnetcore-2-1-identity-ui/ and it also applies to new core 3 angular 8 Template project which have also Angular authentication support.
one thing that is a little different was the public async Task OnPostAsync() method on Areas.Identity.Pages.Account.Manage.Index.cshtml.cs file which is for supporting updating the name of the user in its profile.
So comparing your scaffolded template with below sample may help which uses UpdateAsync instead of previous simple single SetPhoneNumberAsync call.
if (Input.PhoneNumber != phoneNumber || Input.Name != user.FullName)
{
user.FullName = Input.Name;
user.PhoneNumber = Input.PhoneNumber;
var updateProfileResult = await _userManager.UpdateAsync(user);
if (!updateProfileResult.Succeeded)
{
throw new InvalidOperationException($"Unexpected error ocurred updating the profile for user with ID '{user.Id}'");
}
}

MVC Route and Angular

I am facing a problem that is driving me crazy. I will try to be simple about the issue.
I have a project that uses MVC and Angular. The project has a page layout to signin and another to use in main project. I can't change the way the users access the pages, so I need to create the routes the same way they are using.
I want to change the mvc route to use different actions to use the correct layout, one is for signin and the other layout to the main app. The examples below:
To access the signin page: {mainurl}/#/signin
To access another signin page, but for support, using the same layout as signin: {mainurl}/#/signinsupport
To access the main page, after signin: {mainurl}/#/main
How can I change the mvc route to access these different pages using the urls above? Is there an easy way to do that?
Thanks a lot for your help!

How to authorize using windows authentication in ASP.net 5

I am creating an application which is going to use windows authentication to allow access to page based on user and its roles. I have gone ahead and configured IIS to use windows authentication and disabled anonymous access. I am able to browse the application without any issue and I have successfully enabled windows authentication for the application.
Now I am trying to set access to controller level by using Authorize, but I am unable to figure it out how it works with ASP.net 5. I am completely new to ASP / C# programming platform but I did search online resources and I came across this and I used below example to give it a try but I get red squiggly line under users and I think ASP.NET 5 is unable to find users. I also made sure that I have added all the references at the top.
[Authorize(Users = #"CONTOSO\Rick, CONTOSO\Keith, CONTOSO\Mike")]
References:
using Microsoft.AspNet.Mvc;
using RMDAAutomation.Models.Repository;
using RMDAAutomation.Models.Entity;
using Microsoft.AspNet.Authorization;
Any suggestions is highly appreciated. :)
UPDATE 1:
TutorialsController.cs:
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Authorization;
namespace RMDAAutomation.Controllers.Web
{
public class TutorialsController : Controller
{
[Authorize(Roles = #"DOMAIN\GROUP")]
public IActionResult Index()
{
return View();
}
}
}
Tutorial Index View:
<div class="container">
<h1>Tutorials</h1>
</div>
Visual studio project settings:
using System.Web.Mvc;
This using solves the issue in my project

Change role permissions in ASP .NET MVC

I am working on ASP .NET MVC 5 web application. I am using
[Authorize(Roles="administrator")]
public class MyController:Controller{}
Is there a way to dynamically add new authorized roles for the controller? For example retrieve this information from database or something similar, so it be like the following
[Authorize(Roles=db.MyControllerRoles)]
public class MyController:Controller{}
This role configuration is static. However there is nothing preventing you from writing your own authorization filter where you can do whatever you want. You need to implement interface IAuthorizationFilter.
I have done things like this in several projects and it's working fine.

What is the best way to verify cookies are enabled for html 5 (mobile) website using asp.net mvc 3

I know that similar questions have been asked here but I am nevertheless having trouble getting it to work.
I am writing an html 5 website using ASP.NET MVC 3 that is targeted to mobile devices.
I had previously asked another question about how to store and retrieve expensive data and decided to go with Session variables.
I finished the app and started internal testing and all was well until we realized that the web site is not working on some iPhones. After researching I came to realize that Session variables depend on cookies and that some iPhones ship with cookies disabled by default.
Until I come up with a better solution, I want to check to see if cookies are enabled and redirect to an error page.
I was hoping that ALL of the code to do this could be written in a base controller but I could not figure out how to do it. So instead I put the following code into the base controller...
protected void WriteCookie()
{
Response.Cookies.Add(new HttpCookie("cookie_test"));
}
protected bool CookieCheck()
{
return (Request.Cookies["cookie_test"] != null);
}
Then in a home controller I am calling the WriteCookie method before loading the view. The view itself has javascript that is redirecting to one of two controllers / actions. The controller / view that will usually be called (because html 5 could not resolve the location) is the ChangeLocation / Index controller/action. In there I have the following code...
if (!CookieCheck())
{
RedirectToAction("CookieRequired", "Error");
}
Even with cookies disabled it is not redirecting to the above error page.
So what I am looking for is a best practices approach for solving this problem with a fairly complete code sample.
My home controller is pretty much ALWAYS redirecting to another page (in the javascript not the controller). I have a total of about 4 controllers with a total of about 6 actions.
My ideal would be to solve this problem completely in the base controller. If not that then I would love for it to work no matter what valid URL is typed (in other words, even if the web app is not entered through Home / Index). If not that then at least I want what I have shown in my code to work (set the cookie in Home / Index and check for it in the other controller / actions) and work even if the redirect is happening in a controller.

Categories

Resources