How to add asp.net identity to existing web api project? - asp.net-mvc-4

I have 2 projects in my solution. First one is simple mvc project and the other one is web api. There was no pre-written code in web api. I put all logics myself. Now I want to add asp.net identity in the web api project. How can I do that?
Thanks.

In your web api project, you can do this:
1. Create a DbContext class that looks like this:
public class DataContext : IdentityDbContext<IdentityUser>
{
public DataContext() : base("ConnectionStringLocal") { }
}
Add a connection string in your Web.config file
In Package manager console, do Enable-Migrations, Add-Migration IdentityUpdate, Update-Database. This will create a database that has asp.net identity built in.
Please let me know if you have additional questions.

Related

Changing user id from string to int (aspnet core identity)

I am just trying to build a simple blog app on blazor server app. I only included Login, Register and Logout using Identity scaffolder. I am trying to convert user id from string to int. Followed MS documentation and here is what I have done so far:
I created these 2 classes and replaced all the IdentityUser and IdentityRole classes with these in my project like login, program.cs and register
Here is the DBContext
This is program.cs
This error shows up when I run the solution. Any idea on how to resolve this?

How can i add an API in an ASP.NET Core Web App

I have create an "ASP.NET Core Web App" Project in Visual Studio.
I want to add an API in this project ? I can create a separate Web API project but i want to put everything in the same project.
Is it possible ?
Thanks
If you want to keep the web app project and also add a few API endpoints you could just add a folder (e.g. Api) at the project root where you can place your api-controllers.
Then in that folder you can add a class that will derive from ControllerBase and decorate it with [ApiController] attribute and a [Route] attribute.
This requires configuration of controllers in your Startup.cs though:
In ConfigureServices() add a call to services.AddControllers();
In Configure specify the MapControllers() option:
app.UseEndpoints(endpoints => {
// other code
endpoints.MapControllers();
});
I would suggest you build an API solution, and then a separate Core Web App solution.
Otherwise you could try starting with a 'Blank Solution' and adding different projects to it.

Retrieve IIS' ApplicationName in ASP.NET Core 3 application

I'd like to know under what IIS' Application Name my asp.net core 3 application is running at start time. Possibly also Site Name.
There is a misleading IHostingEnvironment.ApplicationName which is something else (assembly name in fact).
Any idea?
As far as I know, there is no build-in method which could get the web application name. Here is a workaround. We could try to use System.Security.Principal.WindowsIdentity.GetCurrent() to get the current identity name.
Normally, the identity name is the IIS web site name if you don't modify it.
Details, you could refer to below codes:
public IActionResult Index()
{
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
return View();
}
Result:
Notice:Remove the IIS application pool is the IIS web site name

Do I have to do the configuration for nservicebus on mvc4 that would be handled by the EndpointConfig.cs?

In the examples, most of the config is done by the dev by changing AsA_Server to AsA_Client.
public class EndpointConfig : IConfigureThisEndpoint, AsA_Client { }
However, I can't seem to do that with an ASP.NET MVC4 app.
Do I have to manually configure everything in a web environment?
Yes, here are links to the documentation:
http://support.nservicebus.com/customer/portal/articles/894008-using-nservicebus-with-asp-net-mvc
http://support.nservicebus.com/customer/portal/articles/894123-injecting-the-bus-into-asp-net-mvc-controller
You can also have a look at our sample projects for examples on how to do it, see https://github.com/NServiceBus/NServiceBus/tree/master/Samples/AsyncPagesMVC3
There is also a sample that uses MVC4 but that is against NServiceBus v4 which has not been released yet, see https://github.com/Particular/NServiceBus/tree/develop/Samples/VideoStore.Msmq/VideoStore.ECommerce

ASP.NET Web API Sample Membership Authentication - Azure DB Initialization

I created a brand new MVC 4 Project (using Web Api Template, not Internet template). How can I initialize the simple membership database at first run exactly as the internet template does? What I did so far:
created brand new MVC 4 Project -> Web Api Template
implemented the filter InitializeSimpleMembershipAttribute.cs and decorated the HomeController with it.
added the model AccountModels.cs
added enabled="true" attribute to roleManager under system.web in web.config
the connection string is pointing to SQL Azure and working fine
When I run this app for the first time, the database is created but are created just 1 table in it: UserProfile, all the other tables: webpages_Membership, webpages_Roles ... are missing. There are no errors nothing.
All I need is to be able that when my MVC4 Project (Web Api Template) runs for the first time, will ensure that the SampleMembership db and tables are created. In order to be able to authenticate a client rest call using membership authentication.
Please let me know if this is the right path.
Have you checked if the connection string name in the web.config is defined
in AccountModels.cs for the usercontext
in InitializeSimpleMembershipAttribute.cs for the database initialization