本文整理汇总了C#中IUserService.GetByUsername方法的典型用法代码示例。如果您正苦于以下问题:C# IUserService.GetByUsername方法的具体用法?C# IUserService.GetByUsername怎么用?C# IUserService.GetByUsername使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IUserService
的用法示例。
在下文中一共展示了IUserService.GetByUsername方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsAuthorized
protected override bool IsAuthorized(HttpActionContext actionContext)
{
if (actionContext == null)
return base.IsAuthorized(actionContext);
bool disableAuthentication = false;
GenericIdentity identity;
GenericPrincipal principal;
string[] roles;
#if DEBUG
disableAuthentication = true;
#endif
if (disableAuthentication)
{
// Create a fake user and use this in debugging to disable authentication.
identity = new GenericIdentity("superuser");
roles = new string[] { "admin" };
principal = new GenericPrincipal(identity, roles);
HttpContext.Current.User = principal;
}
else
{
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
// According to many articles and Ninject docs like:
// https://github.com/ninject/Ninject.Web.Mvc/wiki/Filters-and-Scoped,
// https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=98,
// https://stackoverflow.com/questions/29915192/unity-property-injection-on-authorizeattribute
// MVC caches filters, which means dependency injection with .InRequestScope() does not work.
// If we try to inject the _authorizationFilter with Ninject, there is a runtime error saying the DbContext has been disposed
// on the second and all subsequent requests (works fine on the first request).
using (_userService = new UserService())
{
User myUser = _userService.GetByUsername(authTicket.Name);
if (myUser == null)
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, string.Format("The {0} account was not found (or there was an error looking it up).", authTicket.Name));
if (!myUser.IsActive)
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, string.Format("The {0} account is disabled.", authTicket.Name));
if (myUser.IsAdmin)
roles = new string[] { "admin" };
else
roles = null;
}
identity = new GenericIdentity(authTicket.Name, "Forms");
principal = new GenericPrincipal(identity, roles);
HttpContext.Current.User = principal;
}
}
// Now that we have set httpContext.User appropriately, do the authorization check which will make sure user is in the proper Role.
return base.IsAuthorized(actionContext);
}
示例2: AuthorizeCore
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool disableAuthentication = false;
GenericIdentity identity;
GenericPrincipal principal;
string[] roles;
#if DEBUG
disableAuthentication = true;
#endif
if (disableAuthentication)
{
// Create a fake user and use this in debugging to disable authentication.
identity = new GenericIdentity("alongoria");
roles = new string[] { "admin" };
principal = new GenericPrincipal(identity, roles);
httpContext.User = principal;
}
else
{
HttpCookie authCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
string username = authTicket.Name;
// According to many articles and Ninject docs like:
// https://github.com/ninject/Ninject.Web.Mvc/wiki/Filters-and-Scoped,
// https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=98,
// https://stackoverflow.com/questions/29915192/unity-property-injection-on-authorizeattribute
// MVC caches filters, which means dependency injection with .InRequestScope() does not work.
// If we try to inject the _authorizationFilter with Ninject, there is a runtime error saying the DbContext has been disposed
// on the second and all subsequent requests (works fine on the first request).
using (_userService = new UserService())
{
User user = _userService.GetByUsername(username);
if (user == null)
{
// The user has a cookie, so they are in the Active Directory.
// But they aren't in our local database (new employee, probably), so add them.
User newUser = new User()
{
Username = username,
IsActive = true,
IsAdmin = false
};
HttpCookie initialsCookie = httpContext.Request.Cookies[Configuration.GetAppSetting("UserInitialsCookieName")];
if (initialsCookie != null && !string.IsNullOrWhiteSpace(initialsCookie.Value))
{
newUser.Initials = initialsCookie.Value;
}
else
{
if (username.Length > 1)
newUser.Initials = username.Substring(0, 2);
else
newUser.Initials = "xx";
}
_userService.Insert(newUser);
user = _userService.GetByUsername(username);
using (var log = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger())
{
log.Information("A new user was added to the application: {0}", username);
}
} // End inserting new User and pulling it from the db.
else
{
// If there's a cookie with initials, check its value to make sure our db value isn't out of date.
HttpCookie initialsCookie = httpContext.Request.Cookies[Configuration.GetAppSetting("UserInitialsCookieName")];
if (initialsCookie != null && !string.IsNullOrWhiteSpace(initialsCookie.Value))
{
if (!initialsCookie.Value.Equals(user.Initials, System.StringComparison.CurrentCultureIgnoreCase))
{
user.Initials = initialsCookie.Value;
_userService.Update(user);
}
}
}
if (!user.IsActive)
{
using (var log = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger())
{
log.Warning("A user whose account was disabled attempted to log on to the application: {0}", username);
}
throw new HttpException(500, string.Format("The {0} account has been disabled.", authTicket.Name));
}
if (user.IsAdmin)
roles = new string[] { "admin" };
//.........这里部分代码省略.........