Пример #1
0
 /// <summary>
 /// Get the User by the user name.
 /// </summary>
 /// <param name="username"></param>
 /// <returns></returns>
 public static User GetUserByName(string username)
 {
     using (var db = new SimpleWikiContext())
     {
         return db.Users.Where(u => u.UserName == username).FirstOrDefault();
     }
 }
Пример #2
0
 public static IList<User> GetOAuthAccountsFromUserName(string userName)
 {
     using (var db = new SimpleWikiContext())
     {
         return db.Users.Where(u => (u.UserName == userName && u.Provider != null)).ToList();
     }
 }
Пример #3
0
 /// <summary>
 /// return where the user has a local user account
 /// </summary>
 /// <param name="userid">the user's id to assert.</param>
 public static bool HasLocalAccount(int userid)
 {
     using (var db = new SimpleWikiContext())
     {
         User user = db.Users.Find(userid);
         return user == null || user.HashPwd == null;
     }
 }
Пример #4
0
 /// <summary>
 /// Get the user's articles list.
 /// </summary>
 /// <param name="username"></param>
 public static IList<Article> GetUserArticles(string username)
 {
     using (var db = new SimpleWikiContext())
     {
         User user=db.Users.Where(u=>u.UserName == username).FirstOrDefault();
         if (user != null)
             return db.Articles.Where(a => a.OwnedUserID == user.UserID).ToList();
         else
             return null;
     }
 }
Пример #5
0
        /// <summary>
        /// User create a new article
        /// </summary>
        /// <param name="user"></param>
        /// <param name="title"></param>        
        public static Article CreateArticle(User user, string title)
        {
            using (var db = new SimpleWikiContext())
            {
                Article article = new Article();
                article.Title = title;
                article.OwnedUserID = user.UserID;
                article.PublishTime = DateTime.Now;

                db.Articles.Add(article);
                db.SaveChanges();
                return article;
            }
        }
Пример #6
0
 /// <summary>
 /// Create the user account.
 /// </summary>
 /// <param name="username">the user name</param>
 /// <param name="password">the user password</param>
 /// <returns>return user if create success, or false.</returns>
 public static User CreateAccount(string username, string password)
 {
     using (var db = new SimpleWikiContext())
     {
         User user = new User();
         user.UserName = username;
         user.HashPwd = SecurityUtil.GetMD5Hash(password);
         user.RegTime = DateTime.Now;
         db.Users.Add(user);
         if (db.SaveChanges() > 0)
             return user;
         else
             return null;
     }
 }
Пример #7
0
        protected void Application_Start()
        {
            using (var context = new SimpleWikiContext())
            {
                if (!context.Database.Exists())
                {
                    ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                }
            }

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
Пример #8
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // 将新用户插入到数据库
                using (SimpleWikiContext db = new SimpleWikiContext())
                {
                    SimpleWikiUser user = db.Users.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // 检查用户是否已存在
                    if (user == null)
                    {
                        // 将名称插入到配置文件表
                        db.Users.Add(new SimpleWikiUser { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "用户名已存在。请输入其他用户名。");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Пример #9
0
 /// <summary>
 /// User login process, if login success, return the user, or return the null
 /// </summary>
 /// <param name="username">login user name</param>
 /// <param name="password">login password</param>
 public static User Login(string username, string password)
 {
     using (var db = new SimpleWikiContext())
     {
         string haspwd = SecurityUtil.GetMD5Hash(password);
         return db.Users.Where(u => (u.UserName == username && u.HashPwd == haspwd)).FirstOrDefault();
     }
 }
Пример #10
0
 /// <summary>
 /// return where the user has a local user account.
 /// </summary>
 /// <param name="username">the user's name to assert</param>
 public static bool HasLocalAccount(string username)
 {
     using (var db = new SimpleWikiContext())
     {
         User user = db.Users.Where(u => u.UserName == username).FirstOrDefault();
         return user == null || user.HashPwd == null;
     }
 }