예제 #1
0
        public async System.Threading.Tasks.Task <ActionResult> Register(string username, string password)
        {
            var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();
            var manager   = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore);
            var user      = new Microsoft.AspNet.Identity.EntityFramework.IdentityUser()
            {
                UserName = username
            };

            Microsoft.AspNet.Identity.IdentityResult result = await manager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                //I have some options: log them in, or I can send them an email to "Confirm" their account details.'
                //I don't have email set up this week, so we'll come back to that.
                //This authentication manager will create a cookie for the current user, and that cookie will be exchanged on each request until the user logs out

                var authenticationManager = HttpContext.GetOwinContext().Authentication;
                var userIdentity          = await manager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

                authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties()
                {
                }, userIdentity);
            }
            else
            {
                ViewBag.Error = result.Errors;
                return(View());
            }
            return(RedirectToAction("Index", "Home"));
        }
예제 #2
0
        public async Task <ClaimsIdentity> CreateIdentityAsync(User user, string authenticationType)
        {
            user.ThrowIfNull("user");
            authenticationType.ThrowIfNull("authenticationType");

            var appIdentityUser = IdentityModelFactory.Create(user);
            var claimsIdentity  = await _userManager.CreateIdentityAsync(appIdentityUser, authenticationType);

            user.CopyFrom(appIdentityUser);

            return(claimsIdentity);
        }
예제 #3
0
        public async System.Threading.Tasks.Task <ActionResult> LogOn(string username, string password, bool?staySignedIn, string returnUrl)
        {
            var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();
            var manager   = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore);



            var user = await manager.FindByNameAsync(username);


            bool result = await manager.CheckPasswordAsync(user, password);

            if (result)
            {
                if (user.EmailConfirmed)
                {
                    //I have some options: log them in, or I can send them an email to "Confirm" their account details.'
                    //I don't have email set up this week, so we'll come back to that.

                    //This authentication manager will create a cookie for the current user, and that cookie will be exchanged on each request until the user logs out
                    var authenticationManager = HttpContext.GetOwinContext().Authentication;
                    var userIdentity          = await manager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties()
                    {
                    }, userIdentity);
                }
                else
                {
                    ViewBag.Error = new string[] { "Your email address has not been confirmed." };
                    return(View());
                }
            }
            else
            {
                ViewBag.Error = new string[] { "Unable to Log In, check your username and password" };
                return(View());
            }
            if (string.IsNullOrEmpty(returnUrl))
            {
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                return(Redirect(returnUrl));
            }
        }