راهنمای تغییر بخش احراز هویت و اعتبارسنجی کاربران سیستم مدیریت محتوای IRIS به ASP.NET Identity – بخش دوم
نویسنده: مهدی سعیدی فر
تاریخ: ۱۳۹۴/۰۷/۲۷ ۱۰:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
ToTable("Users"); Get-Project Iris.DomainClasses, Iris.Datalayer, Iris.Servicelayer, Iris.Web | Install-Package Microsoft.AspNet.Identity.EntityFramework
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
public string Description { get; set; }
} public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
public class IrisDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>,
IUnitOfWork public DbSet<ApplicationUser> Users { get; set; } public IrisDbContext()
: base("IrisDbContext")
{
} modelBuilder.Entity<CustomRole>().ToTable("AspRoles");
modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims");
modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles");
modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins"); Add-Migration UpdateDatabaseToAspIdentity
public partial class UpdateDatabaseToAspIdentity : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.UserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.Int(nullable: false),
ClaimType = c.String(),
ClaimValue = c.String(),
ApplicationUser_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Users", t => t.ApplicationUser_Id)
.Index(t => t.ApplicationUser_Id);
CreateTable(
"dbo.UserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.Int(nullable: false),
ApplicationUser_Id = c.Int(),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.Users", t => t.ApplicationUser_Id)
.Index(t => t.ApplicationUser_Id);
CreateTable(
"dbo.UserRoles",
c => new
{
UserId = c.Int(nullable: false),
RoleId = c.Int(nullable: false),
ApplicationUser_Id = c.Int(),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.Users", t => t.ApplicationUser_Id)
.ForeignKey("dbo.AspRoles", t => t.RoleId, cascadeDelete: true)
.Index(t => t.RoleId)
.Index(t => t.ApplicationUser_Id);
CreateTable(
"dbo.AspRoles",
c => new
{
Id = c.Int(nullable: false, identity: true),
Description = c.String(),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
AddColumn("dbo.Users", "EmailConfirmed", c => c.Boolean(nullable: false));
AddColumn("dbo.Users", "SecurityStamp", c => c.String());
AddColumn("dbo.Users", "PhoneNumber", c => c.String());
AddColumn("dbo.Users", "PhoneNumberConfirmed", c => c.Boolean(nullable: false));
AddColumn("dbo.Users", "TwoFactorEnabled", c => c.Boolean(nullable: false));
AddColumn("dbo.Users", "LockoutEndDateUtc", c => c.DateTime());
AddColumn("dbo.Users", "LockoutEnabled", c => c.Boolean(nullable: false));
AddColumn("dbo.Users", "AccessFailedCount", c => c.Int(nullable: false));
}
public override void Down()
{
DropForeignKey("dbo.UserRoles", "RoleId", "dbo.AspRoles");
DropForeignKey("dbo.UserRoles", "ApplicationUser_Id", "dbo.Users");
DropForeignKey("dbo.UserLogins", "ApplicationUser_Id", "dbo.Users");
DropForeignKey("dbo.UserClaims", "ApplicationUser_Id", "dbo.Users");
DropIndex("dbo.AspRoles", "RoleNameIndex");
DropIndex("dbo.UserRoles", new[] { "ApplicationUser_Id" });
DropIndex("dbo.UserRoles", new[] { "RoleId" });
DropIndex("dbo.UserLogins", new[] { "ApplicationUser_Id" });
DropIndex("dbo.UserClaims", new[] { "ApplicationUser_Id" });
DropColumn("dbo.Users", "AccessFailedCount");
DropColumn("dbo.Users", "LockoutEnabled");
DropColumn("dbo.Users", "LockoutEndDateUtc");
DropColumn("dbo.Users", "TwoFactorEnabled");
DropColumn("dbo.Users", "PhoneNumberConfirmed");
DropColumn("dbo.Users", "PhoneNumber");
DropColumn("dbo.Users", "SecurityStamp");
DropColumn("dbo.Users", "EmailConfirmed");
DropTable("dbo.AspRoles");
DropTable("dbo.UserRoles");
DropTable("dbo.UserLogins");
DropTable("dbo.UserClaims");
}
} AddColumn("dbo.Users", "EmailConfirmed", c => c.Boolean(nullable: false, defaultValue:true)); Update-Database
Get-Project Iris.Servicelayer, Iris.Web | Install-Package Microsoft.AspNet.Identity.Owin
x.For<IIdentity>().Use(() => (HttpContext.Current != null && HttpContext.Current.User != null) ? HttpContext.Current.User.Identity : null);
x.For<IUnitOfWork>()
.HybridHttpOrThreadLocalScoped()
.Use<IrisDbContext>();
x.For<IrisDbContext>().HybridHttpOrThreadLocalScoped()
.Use(context => (IrisDbContext)context.GetInstance<IUnitOfWork>());
x.For<DbContext>().HybridHttpOrThreadLocalScoped()
.Use(context => (IrisDbContext)context.GetInstance<IUnitOfWork>());
x.For<IUserStore<ApplicationUser, int>>()
.HybridHttpOrThreadLocalScoped()
.Use<CustomUserStore>();
x.For<IRoleStore<CustomRole, int>>()
.HybridHttpOrThreadLocalScoped()
.Use<RoleStore<CustomRole, int, CustomUserRole>>();
x.For<IAuthenticationManager>()
.Use(() => HttpContext.Current.GetOwinContext().Authentication);
x.For<IApplicationSignInManager>()
.HybridHttpOrThreadLocalScoped()
.Use<ApplicationSignInManager>();
x.For<IApplicationRoleManager>()
.HybridHttpOrThreadLocalScoped()
.Use<ApplicationRoleManager>();
// map same interface to different concrete classes
x.For<IIdentityMessageService>().Use<SmsService>();
x.For<IIdentityMessageService>().Use<IdentityEmailService>();
x.For<IApplicationUserManager>().HybridHttpOrThreadLocalScoped()
.Use<ApplicationUserManager>()
.Ctor<IIdentityMessageService>("smsService").Is<SmsService>()
.Ctor<IIdentityMessageService>("emailService").Is<IdentityEmailService>()
.Setter<IIdentityMessageService>(userManager => userManager.SmsService).Is<SmsService>()
.Setter<IIdentityMessageService>(userManager => userManager.EmailService).Is<IdentityEmailService>();
x.For<ApplicationUserManager>().HybridHttpOrThreadLocalScoped()
.Use(context => (ApplicationUserManager)context.GetInstance<IApplicationUserManager>());
x.For<ICustomRoleStore>()
.HybridHttpOrThreadLocalScoped()
.Use<CustomRoleStore>();
x.For<ICustomUserStore>()
.HybridHttpOrThreadLocalScoped()
.Use<CustomUserStore>(); Install-Package Microsoft.Owin.Host.SystemWeb
using System;
using Iris.Servicelayer.Interfaces;
using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataProtection;
using Owin;
using StructureMap;
namespace Iris.Web
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
configureAuth(app);
}
private static void configureAuth(IAppBuilder app)
{
ObjectFactory.Container.Configure(config =>
{
config.For<IDataProtectionProvider>()
.HybridHttpOrThreadLocalScoped()
.Use(() => app.GetDataProtectionProvider());
});
//ObjectFactory.Container.GetInstance<IApplicationUserManager>().SeedDatabase();
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = ObjectFactory.Container.GetInstance<IApplicationUserManager>().OnValidateIdentity()
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
app.CreatePerOwinContext(
() => ObjectFactory.Container.GetInstance<IApplicationUserManager>());
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",
// clientSecret: "");
//app.UseTwitterAuthentication(
// consumerKey: "",
// consumerSecret: "");
//app.UseFacebookAuthentication(
// appId: "",
// appSecret: "");
//app.UseGoogleAuthentication(
// clientId: "",
// clientSecret: "");
}
}
}