本文整理汇总了C#中IServiceBase.GetSession方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceBase.GetSession方法的具体用法?C# IServiceBase.GetSession怎么用?C# IServiceBase.GetSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceBase
的用法示例。
在下文中一共展示了IServiceBase.GetSession方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Authenticate
public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
{
var user = authService.Request.GetUser();
var userName = user.GetUserName();
if (!LoginMatchesSession(session, userName))
{
authService.RemoveSession();
session = authService.GetSession();
}
if (IsAuthorized(user))
{
session.IsAuthenticated = true;
if (session.UserAuthName == null)
{
session.UserAuthName = userName;
}
var aspReq = (HttpRequestBase)authService.Request.OriginalRequest;
var loginUser = aspReq.ServerVariables["LOGON_USER"].ToNullIfEmpty();
var remoteUser = aspReq.ServerVariables["REMOTE_USER"].ToNullIfEmpty();
var identityName = aspReq.LogonUserIdentity != null ? aspReq.LogonUserIdentity.Name : null;
session.DisplayName = loginUser
?? remoteUser
?? identityName;
var tokens = new AuthTokens {
Provider = Name,
UserName = userName,
DisplayName = session.DisplayName,
Items = new Dictionary<string, string> {
{"LOGON_USER", loginUser},
{"REMOTE_USER", remoteUser},
{"LogonUserIdentityName", identityName},
}
};
if (session.Roles == null)
session.Roles = new List<string>();
foreach (var role in AllRoles.Safe())
{
if (user.IsInRole(role))
session.Roles.AddIfNotExists(role);
}
OnAuthenticated(authService, session, tokens, new Dictionary<string, string>());
return new AuthenticateResponse
{
UserName = userName,
SessionId = session.Id,
DisplayName = session.DisplayName,
ReferrerUrl = request.Continue
};
}
throw HttpError.Unauthorized("Windows Auth failed");
}
示例2: AuthenticateImpl
private object AuthenticateImpl(IServiceBase authService, IAuthSession session, string userName, string password, string referrerUrl)
{
if (!LoginMatchesSession(session, userName))
{
authService.RemoveSession();
session = authService.GetSession();
}
if (TryAuthenticate(authService, userName, password))
{
session.IsAuthenticated = true;
if (session.UserAuthName == null)
{
session.UserAuthName = userName;
}
var response = OnAuthenticated(authService, session, null, null);
if (response != null)
return response;
var bytes = Encoding.UTF8.GetBytes(userName + ":" + password);
return new CustomAuthenticateResponse
{
UserId = session.UserAuthId,
UserName = userName,
SessionId = session.Id,
ReferrerUrl = referrerUrl,
AccessToken = Convert.ToBase64String(bytes)
};
}
throw HttpError.Unauthorized(ErrorMessages.InvalidUsernameOrPassword);
}
示例3: Logout
/// <summary>
/// Remove the Users Session
/// </summary>
/// <param name="service"></param>
/// <param name="request"></param>
/// <returns></returns>
public virtual object Logout(IServiceBase service, Authenticate request)
{
var feature = HostContext.GetPlugin<AuthFeature>();
var session = service.GetSession();
var referrerUrl = (request != null ? request.Continue : null)
?? (feature.HtmlLogoutRedirect != null ? service.Request.ResolveAbsoluteUrl(feature.HtmlLogoutRedirect) : null)
?? session.ReferrerUrl
?? service.Request.GetHeader("Referer")
?? this.CallbackUrl;
session.OnLogout(service);
AuthEvents.OnLogout(service.Request, session, service);
service.RemoveSession();
if (feature != null && feature.DeleteSessionCookiesOnLogout)
{
service.Request.Response.DeleteSessionCookies();
}
if (service.Request.ResponseContentType == MimeTypes.Html && !string.IsNullOrEmpty(referrerUrl))
return service.Redirect(LogoutUrlFilter(this, referrerUrl.SetParam("s", "-1")));
return new AuthenticateResponse();
}
示例4: Authenticate
protected object Authenticate(IServiceBase authService, IAuthSession session, string userName, string password)
{
if (!LoginMatchesSession(session, userName)) {
authService.RemoveSession();
session = authService.GetSession();
}
if (TryAuthenticate(authService, userName, password))
{
session.IsAuthenticated = true;
if (session.UserAuthName == null)
session.UserAuthName = userName;
var response = OnAuthenticated(authService, session, null, null);
if (response != null)
return response;
return new AuthenticateResponse {
UserId = session.UserAuthId,
UserName = userName,
SessionId = session.Id,
};
}
throw HttpError.Unauthorized(ErrorMessages.InvalidUsernameOrPassword);
}
示例5: TryAuthenticate
public virtual bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
var authRepo = authService.TryResolve<IAuthRepository>();
if (authRepo == null) {
Log.WarnFormat("Tried to authenticate without a registered IUserAuthRepository");
return false;
}
var session = authService.GetSession();
var digestInfo = authService.Request.GetDigestAuth();
IUserAuth userAuth;
if (authRepo.TryAuthenticate(digestInfo, PrivateKey, NonceTimeOut, session.Sequence, out userAuth)) {
var holdSessionId = session.Id;
session.PopulateWith(userAuth); //overwrites session.Id
session.Id = holdSessionId;
session.IsAuthenticated = true;
session.Sequence = digestInfo["nc"];
session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
session.ProviderOAuthAccess = authRepo.GetUserAuthDetails(session.UserAuthId)
.ConvertAll(x => (IAuthTokens) x);
return true;
}
return false;
}
示例6: TryAuthenticate
public virtual bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
var authRepo = HostContext.AppHost.GetAuthRepository(authService.Request);
using (authRepo as IDisposable)
{
var session = authService.GetSession();
var digestInfo = authService.Request.GetDigestAuth();
IUserAuth userAuth;
if (authRepo.TryAuthenticate(digestInfo, PrivateKey, NonceTimeOut, session.Sequence, out userAuth))
{
var holdSessionId = session.Id;
session.PopulateWith(userAuth); //overwrites session.Id
session.Id = holdSessionId;
session.IsAuthenticated = true;
session.Sequence = digestInfo["nc"];
session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
session.ProviderOAuthAccess = authRepo.GetUserAuthDetails(session.UserAuthId)
.ConvertAll(x => (IAuthTokens)x);
return true;
}
return false;
}
}
示例7: TryAuthenticate
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
var authRepo = authService.TryResolve<IAuthRepository>().AsUserAuthRepository(authService.GetResolver());
var session = authService.GetSession();
IUserAuth userAuth;
if (authRepo.TryAuthenticate(userName, password, out userAuth))
{
if (IsAccountLocked(authRepo, userAuth))
return false;
// throw new AuthenticationException("This account has been locked");
var holdSessionId = session.Id;
session.PopulateWith(userAuth); //overwrites session.Id
session.Id = holdSessionId;
session.IsAuthenticated = true;
session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
session.ProviderOAuthAccess = authRepo.GetUserAuthDetails(session.UserAuthId)
.ConvertAll(x => (IAuthTokens)x);
return true;
}
return false;
}
示例8: TryAuthenticate
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
Log.Debug("Authenticating {0}...".F(userName));
User user;
if (!AuthenticateUser(userName, password, out user)) return false;
if (user == null) return false;
Log.Debug("Obtaining team for {0}".F(userName));
var team = TeamDbProvider.Instance[user];
if (team == null) return false;
Log.Debug("Obtaining game for {0}".F(userName));
var game = GameDbProvider.Instance[team];
if (game == null) return false;
var session = authService.GetSession();
// PopulateWith() is really great, but it replaces the default
// session ID with the user ID from the database, so save it here
var sessionId = session.Id;
session.PopulateWith(user);
session.Id = sessionId;
session.IsAuthenticated = true;
// These roles will be used later to check what team the user is on, etc.
session.Roles = new List<string> {
User.F(user.Id),
Team.F(team.Id),
Game.F(game.Id)
};
session.UserAuthName = userName;
session.UserAuthId = user.Id.ToString(CultureInfo.InvariantCulture);
var userKey = "User {0}".F(user.Id);
var gameKey = "Game {0}".F(game.Id);
// Cache the user and game objects so they will be easier to
// obtain again later in other service classes (i.e., check-in)
if (Global.Cache.Get<User>(userKey) == null) {
// This cache entry will remain cached for one day
// OR until the user logs out, whichever comes first
Global.Cache.Add(userKey, user, TimeSpan.FromDays(1));
}
if (Global.Cache.Get<Game>(gameKey) == null) {
// This cache entry will stay cached until the
// game ends OR if it is invalidated elsewhere
Global.Cache.Add(gameKey, game, game.Finish);
}
return true;
}
示例9: Authenticate
public override object Authenticate(IServiceBase authService, IAuthSession session, ServiceStack.ServiceInterface.Auth.Auth request)
{
var response = (AuthResponse)base.Authenticate(authService, session, request);
session = authService.GetSession(true);
return new AuthResponseEx
{
ReferrerUrl = response.ReferrerUrl,
ResponseStatus = response.ResponseStatus,
SessionId = response.SessionId,
UserId = session.UserName,
UserName = session.UserAuthName,
};
}
示例10: TryAuthenticate
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
long userId;
if (!AuthenticateUser(userName, password, out userId)) return false;
var user = Users.Get(userId);
var session = authService.GetSession();
session.IsAuthenticated = true;
session.UserAuthId = Convert.ToString(userId);
session.UserAuthName = userName;
session.UserName = "{0} {1}".Fmt(user.FirstName, user.LastName);
session.FirstName = user.FirstName;
session.LastName = user.LastName;
session.Email = user.Email;
session.Roles = new List<string>();
switch (user.Type) {
case UserType.Administrator:
session.Roles.Add(UserType.Administrator.ToString());
goto case UserType.BasicEmployee; // Administrator is also an Employee
case UserType.Shipping:
session.Roles.Add(UserType.Shipping.ToString());
break;
case UserType.BasicEmployee:
session.Roles.Add(UserType.BasicEmployee.ToString());
goto case UserType.User; // BasicEmployees are also regular Users
case UserType.User:
session.Roles.Add(UserType.User.ToString());
break;
}
var perms = new List<string>();
switch (user.Type) {
case UserType.User:
// Users can view products, but so can everyone else, so they don't get a specific permission
break;
case UserType.BasicEmployee:
perms.Add(Permissions.CanCreateOrders);
perms.Add(Permissions.CanManageProducts);
break;
case UserType.Shipping:
break;
case UserType.Administrator:
// Administrators GET ALL THE PERMISSIONS
perms.AddRange(typeof(Permissions).GetFields()
.Where(x => x.FieldType == typeof(string) && x.IsLiteral)
.ToList().Select(x => x.Name));
break;
}
session.Permissions = perms;
return true;
}
示例11: TryAuthenticate
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
var session = authService.GetSession();
var userAuth = this.FindByPasswordCredential(userName, password);
if (userAuth != null)
{
session.UserName = userAuth.UserName;
session.Email = userAuth.Email;
session.DisplayName = userAuth.DisplayName;
session.Permissions = userAuth.Permissions;
session.Roles = userAuth.Roles;
session.IsAuthenticated = true;
session.UserAuthId = userAuth.RefIdStr;
}
return userAuth != null;
}
示例12: Logout
/// <summary>
/// Remove the Users Session
/// </summary>
/// <param name="service"></param>
/// <param name="request"></param>
/// <returns></returns>
public virtual object Logout(IServiceBase service, Auth request)
{
var session = service.GetSession();
var referrerUrl = (request != null ? request.Continue : null)
?? session.ReferrerUrl
?? service.RequestContext.GetHeader("Referer")
?? this.CallbackUrl;
session.OnLogout(service);
service.RemoveSession();
if (service.RequestContext.ResponseContentType == ContentType.Html && !String.IsNullOrEmpty(referrerUrl))
return service.Redirect(referrerUrl.AddHashParam("s", "-1"));
return new AuthResponse();
}
示例13: Authenticate
protected object Authenticate(IServiceBase authService, IAuthSession session, string userName, string password)
{
if (!LoginMatchesSession(session, userName)) {
authService.RemoveSession();
session = authService.GetSession();
}
if (TryAuthenticate(authService, userName, password)) {
if (session.UserAuthName == null) {
session.UserAuthName = userName;
}
OnAuthenticated(authService, session, null, null);
return new AuthenticateResponse {
UserName = userName,
SessionId = session.Id,
};
}
throw HttpError.Unauthorized("Invalid UserName or Password");
}
示例14: TryAuthenticate
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
return false;
using (var db = GetDB(authService))
{
var user = db.FirstOrDefault<Account>(c => c.UserName == userName && c.Password == password);
if (user!=null)
{
var session = (CustomUserSession)authService.GetSession(false);
session.UserName = user.UserName;
session.DisplayName = user.DisplayName;
session.IsAuthenticated = true;
// session.Roles = new List<string>();
// if (session.DisplayName == "admin") session.Roles.Add(RoleNames.Admin);
// session.Roles.Add("User");
return true;
}
return false;
}
}
示例15: TryAuthenticate
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
var user = UserService.GetUser(userName);
authService.GetSession().FillSession(user);
return user.Authenticate(password);
}