No AccountController for asp.net core 2.1 - asp.net-core

I realise in asp.net core 2.1 Identity has been shifted, but can be added to the solution if you add them as a scaffolded item.
It adds all the razor class library for all the pages.
However what I want to do is have the old AccountController way where a client (mobile or web) can post to the account related API's..
What are my options for being able to get the old way back or similar. so that i can use api's from clients

Unfortunately, it doesn't exist any more. I was personally very aggravated by this change, as well as the team's endless pushing of Razor Pages on everyone, which frankly should have been left in the dustbin of history </rant>.
What I have done, personally, is add the scaffold, and then create my own controllers, shuffling and rewriting the Razor Pages code into traditional MVC-style controllers and views. This was not a pleasant experience, though, but I know of no other way if you want it this way.
One thing to note, though, is that the AddDefaultIdentity extension actually adds the default UI, as well, just as if you had scaffolded it into your project. In other words, even if you move everything over to controllers and views and delete all the scaffolded stuff, the Razor Pages routes will still take precedence. Even more unfortunate, there's no good alternative to add AddDefaultIdentity. This is what you'll need instead:
services.AddIdentityCore<ApplicationUser>(o =>
{
// Identity config
})
.AddSignInManager()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<YourContext>();
services.ConfigureApplicationCookie(o =>
{
o.LoginPath = "/signin";
o.LogoutPath = "/signout";
o.AccessDeniedPath = "/signin";
o.ReturnUrlParameter = "returnUrl";
});
Obviously, in the last bit, you'd change the URLs to your own applications routes.

Related

MVC based authentication templates in ASP.NET Core 2.0

When creating a new ASP.Net Core web project in ASP.Net Core 2.0, and choosing the 'Individual account' authentication option, the authentication views/controllers where originally implemented using ASP.Net MVC. Recently it appears they have been updated to use Razor pages. My questions is...is there a way I can revert the new project template to using the MVC instead of Razor pages or at the very least is there a way I can see what code the MVC template used to create?
Simply, no. Identity now comes with a Razor Class Library containing a default UI, which as you've noted, is Razor Pages-based. If you want the old-style MVC setup, you'll need to create it yourself. You can scaffold the Default UI pages into your project and then refer to these to move code into controllers/views. Then, when you're done with that, remove the default UI pages in your project, and turn off the default UI in general by using AddIdentity<TUser, TRole> instead of AddDefaultIdentity<TUser> (which adds the default UI under the hood).
FWIW, I used to be totally opposed to Razor Pages until I endeavor the same thing you're about to embark on. After moving all the code into controllers, I started to remember how much of a mess it actually was. There's so much boilerplate code involved in auth: sign in, sign out, registration, password resets, 2FA, third-party login, etc. You end up with monstrous controllers with hundreds or even thousands of lines of code. Even if you try to break it up into many different controllers, that just kind of makes it worse. Long and short, Razor Pages actually works pretty well for something like this. It keeps each unit of functionality self-contained, so you know exactly where your need to go to edit stuff. I'd encourage you to give it a go as-is, first, and see how it works for you.
Also, one of your main concerns may be the Web Forms style of routing with Razor Pages, where you the path becomes the URL, and if you're like me, that probably offends your sensibilities. This can actually be changed, though it's not documented well at all. You can simply specify whatever route you'd like the page to have with the #page directive. For example, you could do something like following in Login.cshtml:
#page "/signin"
Then, you can access the page via /signin, instead of /Identity/Account/Login.cshtml.

Usage of areas in .NET core

I am developing an ASP.NET MVC Core application. In this application I'm using areas. I have multiple areas now: Administrator (for website maintenance), Identity (for identity razor pages) and Public (for visitors).
I like those areas, but I dont' like the fact that I need my visitors url's look like: https://localhost:5001/Public/Home and https://localhost:5001/Public/Catalogue, etc.
I more like urls like https://localhost:5001/Home and https://localhost:5000/Catalogue etc.
Is there any possibility, where I can use areas, except for the visitor part of the website?
I will not recommend to get rid of "area name" in the url, because the routing will not work properly when you have similar page names in different areas! e.g. if you have two areas "public" and "private" and both contains pages with similar name "Index" most probably you will see an error that there is multiple pages using the same route, and thats why areas are exists.
back to your issue, if you are using razor pages you may add the route template directly to the top of razor page:
#page "/catalog"
or if you have parameters:
#page "{id}"
#Url.Page("/catalog")
if you are using MVC, then you may use attribute routing and keep AreaPrefix empty:
[RouteArea("Public", AreaPrefix = "")]
[RoutePrefix("catalog")]
public class CatalogsController : Controller
you can read more about routing in the docs here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.2

Laravel 5.2 custom Middleware Levels on RESTful controllers

So from not using any framework at all, I finally forced myself to use LARAVEL 5.2 because i got tired of re-writing my own "framework" over and over again.
Anyway!
I think I'm pretty familiar with the Laravel framework and its dependencies now.
But now I need guidance on how to do this the smartest way.
I want to create a Middleware based on the companys DC.
Now every user is AUTHED by php's envget("username"); with a re-written "auth" middleware. So far, so good. (The server is not in the DMZ btw).
The thing is, our team thought that we should populate a database-table with all the users and give them a "Privilege-level", lets say 1 through 3 where 1 is "read only", 2 is "read+modify" and 3 is "read+create+modify" with our restful controllers. But.
since we use restful controllers i cannot really give them individual Middlewares? do I manually have to change every RESTful resource in my routes.php to make this happend? or can i do this another way? I would like to keep it as simple as possible.
Any suggestions?
Thanks in advance
(reserved for typsos )
#Tarre Tan
You can achieve this by adding your middleware in a parent controller's constructor.
All your controllers that require permissions will inherit from that controller.
You have one place to adjust your permissions if you want.
Hope this helps

Mixing Web Api and ASP.Net MVC Pages in One Project

How do I mix Web API and ASP.Net MVC pages in one project?
For instance, I have model User. I would like, within the same project, to have an ApiController that would respond to all the HTTP verbs for managing the User entities, and at the same time have a Controller that would return the appropriate strongly-typed Views depending on the Action requested.
I can't name both controllers UserController. What is the best way around this? Should I name one UserApiController and the other UserController? Any other suggestions?
You can put them in separate namespaces, e.g MyApp.Controllers.UsersController and MyApp.Controllers.WebAPI.UsersController.
This would let you expose similar URI routes in MVC and WebAPI, eg:
/users/1 << MVC view
/api/users/1 << Web API
I haven't changed the namespaces, the only thing that I had to do was register WebApi first, and then MVC route
//First register WebApi router
GlobalConfiguration.Configure(WebApiConfig.Register);
//and register Default MVC Route after
RouteConfig.RegisterRoutes(RouteTable.Routes);
and everything works great!
The WebApi implementation should be added as a separate Area in the MVC application. It is a natural fit. Doing this gives you the separate namespace that Mike Wasson recommended, plus it gives you a natural way to set up the /api routing. You get a separate model folder
Additionally, it is very specifically separated from the rest of the project. If requirements in the future are ever such that you need to separate the api implementation into a separate project, having the api implementation isolated to a separate area makes that breaking it out a lot easier.

Automatic non-RESTful routes in Rails 3?

We have an app with a large number of non-RESTful, verb-oriented controllers. I remember that long ago, before Rails got the REST religion, you could get automatic routes and helpers for those. Is there any way to still do this? It's a pain to keep adding GETs every time we add an action, and our app (or perhaps our collective development brain) just doesn't lend itself to RESTfulness.
You can use the "magic route", I believe it's still in the routes file by default, but if you don't have it here it is:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
You can still use a default route like this:
match ':controller(/:action(/:id))'
to match paths like
/monkey/play
/monkey/see/1
/monkey/hear/1
/monkey/eat/1