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


C# UserService.GetByUsername方法代码示例

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


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

示例1: ToggleFavorite

        public ActionResult ToggleFavorite(int id)
        {
            var userService = new UserService();

            var snippet = _snippetService.GetById(id);
            var user = userService.GetByUsername(User.Identity.Name);
            var favorite = user.Favorites.SingleOrDefault(s => s.Snippet.SnippetId == snippet.SnippetId);

            if (favorite != null) {
                snippet.Favorited--;
                user.Favorites.Remove(favorite);
            }
            else {
                snippet.Favorited++;
                user.Favorites.Add(new Favorite {
                    DateCreated = DateTime.Now,
                    Snippet = snippet,
                    User = user
                });
            }

            userService.Save();

            if (Request.IsAjaxRequest())
                return Json(new {success = true });

            return Redirect(snippet.Link);
        }
开发者ID:simplyio,项目名称:snippet-box,代码行数:28,代码来源:SnippetController.cs

示例2: Application_AcquireRequestState

        protected void Application_AcquireRequestState(Object sender, EventArgs e)
        {
            if (!(Context.Handler is IRequiresSessionState)) return;
            if (Context.User == null || !Context.User.Identity.IsAuthenticated || Context.Session == null) return;

            try {
                User user = null;
                if (Context.Session["User"] != null) {
                    user = Context.Session["User"] as User;
                }
                else {

                    UserService userService = new UserService();
                    user = userService.GetByUsername(Context.User.Identity.Name);

                    if (user == null) {
                        FormsAuthentication.SignOut();
                        return;
                    }
                    Context.Session.Add("User", user);

                }

                Thread.CurrentPrincipal = Context.User = user;
            }
            catch { }
        }
开发者ID:simplyio,项目名称:snippet-box,代码行数:27,代码来源:Global.asax.cs

示例3: Create

        public ActionResult Create(SnippetModel snippetModel)
        {
            if (!ModelState.IsValid)
                return View(snippetModel);

            var userService = new UserService();
            var categoryService = new CategoryService();

            var snippet = new Snippet();
            snippet.BodyRaw = snippetModel.Snippet.BodyRaw;
            snippet.Body = new Markdown().Transform(snippetModel.Snippet.BodyRaw).ToSafeHtml();
            snippet.DateCreated = DateTime.Now;
            snippet.DateEdited = DateTime.Now;
            snippet.Title = snippetModel.Snippet.Title;
            snippet.Slug = snippetModel.Snippet.Title.Slugify();
            snippet.Author = userService.GetByUsername(User.Identity.Name);
            snippet.Category = categoryService.GetById(snippetModel.CategoryId.Value);

            _snippetService.Create(snippet);
            _snippetService.Save();

            TempData["Message"] = "The snippet has been created. <a href=\"{0}\">View the snippet.</a>".FormatWith(snippet.Link);
            return RedirectToAction("Index");
        }
开发者ID:simplyio,项目名称:snippet-box,代码行数:24,代码来源:SnippetsController.cs

示例4: AuthorizeCore

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //If not authenticated, it might be a request from flash in Firefox, so get the auth token passed in to create Identity
            if (!httpContext.Request.IsAuthenticated)
            {
                var token = httpContext.Request.Params[TokenKey];
                if (token != null)
                {
                    var ticket = FormsAuthentication.Decrypt(token);
                    if (ticket != null)
                    {
                        var identity = new FormsIdentity(ticket);
                        httpContext.User = new GenericPrincipal(identity, null);	//this doesn't need to be a UserPrincipal, because that will happen below
                    }
                }
            }

            if (!httpContext.Request.IsAuthenticated)
                return false;

            // If it's not a UserPrincipal, we need to create it (b/c this happens before BaseController.OnAuthorization)
            if (!(httpContext.User is UserPrincipal))
            {
                User user = null;
                if (httpContext.User.Identity.IsAuthenticated && httpContext.User.Identity.AuthenticationType == "Forms")
                {
                    using (var db = ObjectFactory.GetInstance<SqlConnection>())
                    {
                        db.Open();
                        var userService = new UserService(db, Cache);
                        user = userService.GetByUsername(httpContext.User.Identity.Name);
                    }
                    if (user == null || user.IsDeleted)
                        return false;
                }
                else
                {
                    user = new User();
                }

                var identity = httpContext.User != null ? httpContext.User.Identity : new GenericIdentity(user.Username ?? string.Empty);
                httpContext.User = new UserPrincipal(user, identity);

                Thread.CurrentPrincipal = httpContext.User;
            }

            var userObject = httpContext.User as UserPrincipal;

            return !RequireAdmin || userObject.IsAdmin;
        }
开发者ID:Relix1990,项目名称:MvcKickstart,代码行数:50,代码来源:RestrictedAttribute.cs


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