当前位置: 首页>>代码示例>>C#>>正文


C# UserManager.Find方法代码示例

本文整理汇总了C#中UserManager.Find方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.Find方法的具体用法?C# UserManager.Find怎么用?C# UserManager.Find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UserManager的用法示例。


在下文中一共展示了UserManager.Find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetupRolesAndUsers

        private static void SetupRolesAndUsers(DbContext context)
        {
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            // add roles
            if (!roleManager.RoleExists(Role.Guest.ToString()))
                roleManager.Create(new IdentityRole(Role.Guest.ToString()));
            if (!roleManager.RoleExists(Role.Supplier.ToString()))
                roleManager.Create(new IdentityRole(Role.Supplier.ToString()));
            if (!roleManager.RoleExists(Role.Deactivated.ToString()))
                roleManager.Create(new IdentityRole(Role.Deactivated.ToString()));
            if (!roleManager.RoleExists(Role.User.ToString()))
                roleManager.Create(new IdentityRole(Role.User.ToString()));
            var adminRole = roleManager.FindByName(Role.Admin.ToString());
            if (adminRole == null)
            {
                adminRole = new IdentityRole(Role.Admin.ToString());
                roleManager.Create(adminRole);
            }
            #if DEBUG
            //add admin user
            var admin = userManager.Find(Admin_User, Admin_Pass);
            if (admin == null)
            {
                admin = new ApplicationUser
                {
                    UserName = Admin_User,
                    Email = Admin_Mail,
                    EmailConfirmed = true
                };
                var result = userManager.Create(admin, Admin_Pass);
                // TODO: verify returned IdentityResult
                userManager.AddToRole(admin.Id, Role.Admin.ToString());
                result = userManager.SetLockoutEnabled(admin.Id, false);
            }

            var rolesForUser = userManager.GetRoles(admin.Id);
            if (!rolesForUser.Contains(adminRole.Name))
            {
                var result = userManager.AddToRole(admin.Id, adminRole.Name);
            }

            //add normal user
            if (userManager.Find("[email protected]", "1q2w3e4r") == null)
            {
                var user = new ApplicationUser
                {
                    UserName = "[email protected]",
                    Email = "[email protected]",
                    EmailConfirmed = true
                };
                userManager.Create(user, "1q2w3e4r");
                // TODO: verify returned IdentityResult
                userManager.AddToRole(user.Id, Role.User.ToString());
            }
            #endif
        }
开发者ID:uaandrei,项目名称:d4232ffg,代码行数:57,代码来源:DbConfig.cs

示例2: LogIn

 protected void LogIn(object sender, EventArgs e)
 {
     if (IsValid)
     {
         // Validate the user password
         var manager = new UserManager();
         var returnUrl = Request.QueryString["ReturnUrl"];
         ApplicationUser user = manager.Find(UserName.Text, Password.Text);
         if (user != null)
         {
             IdentityHelper.SignIn(manager, user, RememberMe.Checked);
             if (returnUrl == null)
             {
                 IdentityHelper.RedirectToReturnUrl("~/Game/User-Home.aspx", Response);
             }
             else
             {
                 IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
             }
         }
         else
         {
             FailureText.Text = "Invalid username or password.";
             ErrorMessage.Visible = true;
         }
     }
 }
开发者ID:masterhou,项目名称:webgl2012,代码行数:27,代码来源:Login.aspx.cs

示例3: btnSignIn_OnClick

    protected void btnSignIn_OnClick(object sender, EventArgs e)
    {
        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.
            ConnectionStrings["GarageDBConnectionString"].ConnectionString;

        UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);

        //to retrieve a user from the database
        var user = manager.Find(txtUserName.Text, txtPassword.Text);

        if (user != null)
        {
            //Call OWIN functionality
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            //Sign in user
            authenticationManager.SignIn(new AuthenticationProperties
            {
                IsPersistent = false
            }, userIdentity);

            //Redirect user to homepage
            Response.Redirect("~/Index.aspx");
        }
        else
        {
            litStatus.Text = "Invalid username or password";
        }
    }
开发者ID:SaifAsad,项目名称:garager,代码行数:33,代码来源:Login.aspx.cs

示例4: btnLogin_Click

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.ConnectionStrings["CANDZOILPDBConnectionString"].ConnectionString;

        UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);

        var user = manager.Find(txtUserName.Text, txtPassword.Text);

        if (user != null) {
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

            var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            authenticationManager.SignIn(new AuthenticationProperties{
                IsPersistent = false
            }, userIdentity);

            Response.Redirect("~/Index.aspx");
        }else{
            litStatus.Text = "Invalid username or password";
        }
    }
开发者ID:Rita124,项目名称:Rita124.github.io,代码行数:25,代码来源:Login.aspx.cs

示例5: SignIn

        protected void SignIn(object sender, EventArgs e)
        {
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);
            var user = userManager.Find(UserName.Text, Password.Text);
            try
            {
                if (user != null)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("admin/main-menu.aspx");
                }
                else
                {
                    StatusText.Text = "Invalid username or password.";
                    LoginStatus.Visible = true;
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
开发者ID:Bennyva,项目名称:comp2007-lab4,代码行数:26,代码来源:login.aspx.cs

示例6: Validate

 public override void Validate(string userNameOrEmail, string password)
 {
     try
     {
         using (var context = new IdentityDbContext())
         {
             using (var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(context)))
             {
                 string userName = userNameOrEmail;
                 if (userNameOrEmail.Contains('@'))
                 {
                     var userForEmail = userManager.FindByEmail(userNameOrEmail);
                     if (userForEmail != null)
                     {
                         userName = userForEmail.UserName;
                     }
                 }
                 var user = userManager.Find(userName, password);
                 if (user == null)
                 {
                     var msg = String.Format("Unknown Username {0} or incorrect password {1}", userNameOrEmail, password);
                     Trace.TraceWarning(msg);
                     throw new FaultException(msg);
                 }
             }
         }
     }
     catch (Exception e)
     {
         var msg = e.Message;
         Trace.TraceWarning(msg);
         throw new FaultException(msg);
     }
 }
开发者ID:minrogi,项目名称:PLMPack,代码行数:34,代码来源:IdentityValidator.cs

示例7: AuthModule

        public AuthModule()
            : base("/api")
        {
            Post["/authenticate"] = x => {
                var bind = this.Bind<LoginRequest>();

                UserManager<User> manager = new UserManager<User>(new UserStore());

                var user = manager.Find(bind.Username, bind.Password);

                if (user == null)
                {
                    return new Response
                    {
                        StatusCode = HttpStatusCode.Unauthorized
                    };
                }
                else
                {
                    var response = new Response
                    {
                        StatusCode = HttpStatusCode.OK
                    };
                    return response.WithCookie("sq-valid", user.UserName, DateTime.Now.AddMinutes(5));
                }
            };

            Get["/logout"] = x => {
                var response = new Response
                {
                    StatusCode = HttpStatusCode.OK
                };
                return response.WithCookie("sq-valid", null, DateTime.Now.AddYears(-5));
            };
        }
开发者ID:fugaku,项目名称:StockQuote,代码行数:35,代码来源:AuthModule.cs

示例8: btnSave_Click

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //try
            //{
                var userStore = new UserStore<IdentityUser>();
                var userManager = new UserManager<IdentityUser>(userStore);
                var user = userManager.Find(txtUserName.Text, txtPassword.Text);

                if (user != null)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("~/admin/MainMenu.aspx");
                }
                else
                {
                    lblStatus.Text = "Invalid username or password.";

                }
            //}
            //catch (System.Exception)
            //{
            //    Response.Redirect("/MainMenu.aspx");
            //}
        }
开发者ID:Leathel,项目名称:Lesson12,代码行数:27,代码来源:Login.aspx.cs

示例9: btnLogin_Click

        /**
          Login - authenticate entered user credientials.
         **/
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                var userStore = new UserStore<IdentityUser>();
                var userManager = new UserManager<IdentityUser>(userStore);
                var user = userManager.Find(txtUsername.Text, txtPassword.Text);

                if (user != null)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
                    Response.Redirect("admin/bibleMenu.aspx");
                }
                else
                {
                    lblStatus.Text = "Invalid username or password.";
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/errors.aspx");
            }
        }
开发者ID:JMGeear,项目名称:Comp2007Assignment2,代码行数:29,代码来源:login.aspx.cs

示例10: LoginButton_Click

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            // create new userStore and userManager objects
            var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager<IdentityUser>(userStore);

            // search for and create a new user object
            var user = userManager.Find(UserNameTextBox.Text, PasswordTextBox.Text);

            // if a match is found for the user
            if(user != null)
            {
                // authenticate and login our new user
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                // Sign the user
                authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);

                // Redirect to Main Menu
                Response.Redirect("~/Contoso/MainMenu.aspx");
            }
            else
            {
                // throw an error to the AlertFlash div
                StatusLabel.Text = "Invalid Username or Password";
                AlertFlash.Visible = true;
            }
        }
开发者ID:GeorgianCollege,项目名称:COMP2007-S2016-Lesson6C,代码行数:29,代码来源:Login.aspx.cs

示例11: LogIn

        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                // Validate the user password
                var manager = new UserManager();
                ApplicationUser user = manager.Find(UserName.Text, Password.Text);

                if (user != null)
                {
                    IdentityHelper.SignIn(manager, user, RememberMe.Checked);

                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["J1500ConnectionString"].ConnectionString);
                    conn.Open();
                    SqlCommand cmd3 = new SqlCommand("Update AspNetUsers set LastLogin=GETDATE() where id='" + user.Id + "'", conn);
                    cmd3.ExecuteNonQuery();
                    conn.Close();

                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    FailureText.Text = "Invalid username or password.";
                    ErrorMessage.Visible = true;
                }
            }
        }
开发者ID:kingtid,项目名称:J1500RunningLog,代码行数:27,代码来源:Login.aspx.cs

示例12: LogIn

    protected void LogIn(object sender, EventArgs e)
    {
        if (IsValid)
        {
            // Validate the user password
            var manager = new UserManager();
            ApplicationUser user = manager.Find(UserName.Text, Password.Text);
            if (user != null)
            {
                IdentityHelper.SignIn(manager, user, RememberMe.Checked);
                // call db to get number of saved cars for logged in user
                userName = UserName.Text;
                if (!userName.Equals(""))
                {
                    Application["userName"] = userName;

                    loadNextPage(userName);
                }
            }
            else
            {
                FailureText.Text = "Invalid username or password.";
                ErrorMessage.Visible = true;
            }
        }
    }
开发者ID:rgg1992,项目名称:Test,代码行数:26,代码来源:Login.aspx.cs

示例13: btnLogin_Click

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //declare the collection of users
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            //declare the user manager
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
            //try to find the user
            IdentityUser user = manager.Find(txtEmpNum.Text, txtPassword.Text);
            if (user == null)
                lblStatus.Text = "Username or Password is incorrect";
            else
            {
                if (txtEmpNum.Text == "Administrator")
                {
                    IdentityResult userResult = manager.AddToRole(user.Id, "Admin");
                }
                //add user to role
                //authenticate user
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                authenticationManager.SignIn(userIdentity);
                Response.Redirect("~/MainPage.aspx");

            }
        }
开发者ID:amoryjh,项目名称:nbd_asp,代码行数:25,代码来源:Default.aspx.cs

示例14: GetProfileDetail

        //public OrderDetail UpdateOrderDetail(string id,DateTime deliveryTime, int )
        //{

        //    mergedEntities db = new mergedEntities();
        //    OrderDetail orderDetail = db.OrderDetails.Where(o => o.Id == id)
        //                    .FirstOrDefault();
        //    orderDetail.orderNumber = productID;
        //    orderDetail.deliveryTime = deliveryTime;

        //    db.SaveChanges();
        //    return orderDetail;
        //}




        //Get profile details
        // change public AspNetUser to public RegisteredUser
        public RegisteredUser GetProfileDetail(Login login)
        {
            UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
            IdentityUser identityUser = manager.Find(login.UserName, login.Password);
            mergedEntities db = new mergedEntities();
            RegisteredUser USER = new RegisteredUser();

            var query =
            from a in db.AspNetUsers
            where (a.Id == identityUser.Id)
             select new
            {
                ID = a.Id,
                UserName = a.UserName,
                PhoneNumber = a.PhoneNumber,
                Email = a.Email,           
            };

            foreach (var item in query)
            {
                USER.Id = item.ID;
                USER.UserName = item.UserName;
                USER.TelNumber = item.PhoneNumber;
                USER.Email = item.Email;
            }

            return USER;
        }
开发者ID:cj8397,项目名称:2hourVer2,代码行数:47,代码来源:AccountRepo.cs

示例15: Index

            public ActionResult Index(Login login)
            {
                // UserStore and UserManager manages data retreival. 
                UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
                UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
                IdentityUser identityUser = manager.Find(login.UserName,
                                                                 login.Password);

                if (ModelState.IsValid)
                {
                    if (ValidLogin(login))
                    {
                        IAuthenticationManager authenticationManager
                                               = HttpContext.GetOwinContext().Authentication;
                        authenticationManager
                       .SignOut(DefaultAuthenticationTypes.ExternalCookie);

                        var identity = new ClaimsIdentity(new[] {
                                            new Claim(ClaimTypes.Name, login.UserName),
                                        },
                                            DefaultAuthenticationTypes.ApplicationCookie,
                                            ClaimTypes.Name, ClaimTypes.Role);
                        // SignIn() accepts ClaimsIdentity and issues logged in cookie.  
                        authenticationManager.SignIn(new AuthenticationProperties
                        {
                            IsPersistent = false
                        }, identity);
                        return RedirectToAction("SecureArea", "Home");
                    }
                }
                return View();
            }
开发者ID:JayOwl,项目名称:Bruteforce-Attack-Prevention,代码行数:32,代码来源:HomeController.cs


注:本文中的UserManager.Find方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。