本文整理汇总了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);
}
示例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 { }
}
示例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");
}
示例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;
}