ASP.NET Web API Sample Membership Authentication - Azure DB Initialization - asp.net-mvc-4

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

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?

Random ASP.NET Core Identity Logouts in production

I have an ASP.NET Core MVC web application that uses Identity to handle user account Authentication and Authorization. When running the app on my local IIS express everything was working properly. After deploying the app to a shared web server I started to notice that the logged-in user accounts would get logged out at seemingly random intervals. Through experimentation, I was able to determine that the log-outs were occurring whether the account was active or idle. They were occuring at no recuring time interval and completely unrelated to any expiry time that I set on my cookies. The logouts were occuring on every view in the web app so I couldn't pin the issue to any particular controller. Also I use the same Database for the published and the local testing version of the app and therefore the same user accounts. I anyone has an idea where to start looking for a solution it would be greatly appreciated.
I posted this question because there is a great answer that 90% solves the issue Here however of the multiple forums that I have been scouring over the last few days there are none with an accepted answer. I am posting this answer to address this. The underlying cause of the issue is that IIS application pool is being reset or recyling and on a shared host with multiple applications using it this can be happening fairly frequently. As is suggested in the above link Data Protection has to be used to persist the keys if IIS application pool recycles. Here is the code offered in the original answer.
services.AddDataProtection()
.PersistKeysToFileSystem(new System.IO.DirectoryInfo("SOME WHERE IN STORAGE"))
//.ProtectKeysWithCertificate(new X509Certificate2());
.SetDefaultKeyLifetime(TimeSpan.FromDays(90));
This code is to be added in ConfigureServices in Startup.cs
As my application is being hosted on a shared server using .PersistKeysToFileSystem was not an option so instead I persisted the keys using DbContext like this:
services.AddDataProtection().PersistKeysToDbContext<MyKeysContext>()
.SetDefaultKeyLifetime(TimeSpan.FromDays(90));
Based on This article here I build MyKeysContext as follows.
// Add a DbContext to store your Database Keys
services.AddDbContext<MyKeysContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("MyKeysConnection")));
In ConfigureServices in Startup.cs and then created a class called MyKeysContext as follows:
using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using WebApp1.Data;
namespace WebApp1
{
public class MyKeysContext : DbContext, IDataProtectionKeyContext
{
// A recommended constructor overload when using EF Core
// with dependency injection.
public MyKeysContext(DbContextOptions<MyKeysContext> options)
: base(options) { }
// This maps to the table that stores keys.
public DbSet<DataProtectionKey> DataProtectionKeys { get; set; }
}
}
I created the database on my Host this will probably be different so I have omitted this step. then I applied the migrations to the database like this.
Add-Migration AddDataProtectionKeys -Context MyKeysContext
Update-Database -Context MyKeysContext

.net core authentication using System.DirectoryServices

I want to implement active directory authentication in my .net core application.
When I read https://github.com/dotnet/corefx/issues/2089 this mentions that out of the box System.DirectoryServices is not supported by .net core. So as per the post I added preview for System.DirectoryServices.AccountManagement.
After adding this how do I create a ldap connection. I am not able to create a one as it always returns null or gives object reference error.
I try to create connection as:
var context = new PrincipalContext(ContextType.Domain, "xx.com", username, password);
Any help is appreciated

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

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.

Creating ASP.NET MVC4 project to connect to SiteCore Master for Admin Access

I have a SiteCore 6.5 already installed, and I just want to create a simple ASP.NET MVC4 application that will connect to the Sitecore\Admin, and allow me to call Context.GetItem("/sitecore/content/home") function call. Is there an easy of doing this? What do I have to change in the web.config to make this happen? This is used as a simple Admin toolbox app, so all of the username and password are hardcoded for the connection, and I am just using the SecurityDisabler() and UserSwithcher() to run the function under Admin.
Take a look at the Sitecore Item Web API module, it's available on the SDN. The API gives access to content through items paths, IDs, and Sitecore queries.
The Item Web API was the subject of a presentation at this month's London User Group meeting. You can find a recording of the presentation on YouTube and the demo code is available on GitHub.
If you want to understand what is required to use the standard Sitecore API's to access content items outside of the website context Mike Edwards has a great post on How to configure Sitecore 6.5 to work in NUnit. This post explains what configuration will be required in the caller to access Sitecore without a context and it will then be possible to execute code like the following:
using NUnit.Framework;
using Sitecore.Data;
using Sitecore.Data.Items;
[TestFixture]
public class when_connecting_to_sitecore_master_database
{
[Test]
public void it_can_retrieve_the_root_item()
{
Database db = global::Sitecore.Configuration.Factory.GetDatabase("master");
Item item = db.GetItem("/sitecore");
Assert.IsNotNull(item);
Assert.AreEqual("sitecore", item.Name.ToLower());
}
}
I was able to connect a MVC 4 app to the Sitecore 6.5 installation for some admin implementations. I just copied the entire folder for App_Config and Web.Config file to the MVC app, and the start to migrate the MVC4 web.config into the copied web.config from the installations. I then started to take out pieces one at a time, most of the part that you should take out will show up in the as error when you run the application, but the main part that we giving me an error was the httpredirection to the sitecore installation, and that was the httphandlers tag and httpModules tag, and my MVC app showed right now, and I was able to connect to the Sitecore configuration and factory.