Thursday, 30 June 2016

Signin directly without login with Owin SignInManager

No comments
UserManager :-

public SearchUserManager(IUserStore<ApplicationUser, int> store) : base(store)
        {
            this.UserValidator = new UserValidator<ApplicationUser, int>(this)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
            // Configure validation logic for passwords
            this.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };
         
            this.EmailService = new EmailService();

            var dataProtectionProvider = Startup.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                this.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, int>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
        }


Controller

private readonly SearchUserManager _userManager;

public async Task<ActionResult> Check(string id)
        {
            if (id != null)
            {
                    var user = _userManager.FindById(id);
                    if (user != null)
                    {
                        await SignInAsync(user, false);                      
                    }
                    return RedirectToAction("Home");
             }
            return View();
        }

 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {            SignInManager.AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            SignInManager.AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }