Thursday, 25 May 2017

Owin Add role during registration page

Use the code manager.AddToRole to add role during registration

 if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                string code = manager.GenerateEmailConfirmationToken(user.Id);
                string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
                manager.AddToRole(user.Id, "Hr");
           

How to assign role authentication to folder

This is actually not the full part. This is just a part to add in web.config file.

In order to assign "Admin" role the the folder "SuperAdmin" add the following under System.web tag

<System.web>
<location path="SuperAdmin">
    <system.web>
      <authorization>
        <allow roles="Admin"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
  

Owin Change Cookie name

To change the default cookie name generated by Owin add cookiename using the following steps
1) Go to App_Start/Startup.Auth.cs
2) Add CookieName="yourCookieName"
3) Add an expiration date if you want

// For more information on configuring authentication, please visit 
        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            // 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
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                },
                CookieName="MyCookieName",
                ExpireTimeSpan= System.TimeSpan.FromHours(24)
            });
 


Owin Identity change default table

In Order to change the default table generated by Owin Identity. Add the method OnModelCreating as below

Model/IdentityModel.cs 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        //Create Custom table
        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>().ToTable("BE_Users");
            modelBuilder.Entity<ApplicationUser>().ToTable("BE_Users");

            modelBuilder.Entity<IdentityRole>().ToTable("BE_Roles");
            modelBuilder.Entity<IdentityUserRole>().ToTable("BE_UserRoles");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("BE_UserClaims");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("BE_UserLogin");
        }