本文整理汇总了C#中IAuthSession类的典型用法代码示例。如果您正苦于以下问题:C# IAuthSession类的具体用法?C# IAuthSession怎么用?C# IAuthSession使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IAuthSession类属于命名空间,在下文中一共展示了IAuthSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnAuthenticated
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, System.Collections.Generic.Dictionary<string, string> authInfo)
{
base.OnAuthenticated(authService, session, tokens, authInfo);
if (session.Email == AuthTestsBase.AdminEmail)
session.Roles.Add(RoleNames.Admin);
}
示例2: Authenticate
public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
{
IAuthTokens tokens = Init(authService, ref session, request);
IRequest httpRequest = authService.Request;
string error = httpRequest.QueryString["error"];
bool hasError = !error.IsNullOrEmpty();
if (hasError)
{
Log.Error($"Odnoklassniki error callback. {httpRequest.QueryString}");
return authService.Redirect(FailedRedirectUrlFilter(this, session.ReferrerUrl.SetParam("f", error)));
}
string code = httpRequest.QueryString["code"];
bool isPreAuthCallback = !code.IsNullOrEmpty();
if (!isPreAuthCallback)
{
string preAuthUrl = $"{PreAuthUrl}?client_id={ApplicationId}&redirect_uri={CallbackUrl.UrlEncode()}&response_type=code&layout=m";
this.SaveSession(authService, session, SessionExpiry);
return authService.Redirect(PreAuthUrlFilter(this, preAuthUrl));
}
try
{
string payload = $"client_id={ApplicationId}&client_secret={SecretKey}&code={code}&redirect_uri={CallbackUrl.UrlEncode()}&grant_type=authorization_code";
string contents = AccessTokenUrlFilter(this, AccessTokenUrl).PostToUrl(payload, "*/*", RequestFilter);
var authInfo = JsonObject.Parse(contents);
//ok.ru does not throw exception, but returns error property in JSON response
string accessTokenError = authInfo.Get("error");
if (!accessTokenError.IsNullOrEmpty())
{
Log.Error($"Odnoklassniki access_token error callback. {authInfo}");
return authService.Redirect(session.ReferrerUrl.SetParam("f", "AccessTokenFailed"));
}
tokens.AccessTokenSecret = authInfo.Get("access_token");
tokens.UserId = authInfo.Get("user_id");
session.IsAuthenticated = true;
return OnAuthenticated(authService, session, tokens, authInfo.ToDictionary())
?? authService.Redirect(SuccessRedirectUrlFilter(this, session.ReferrerUrl.SetParam("s", "1")));
}
catch (WebException webException)
{
//just in case it starts throwing exceptions
HttpStatusCode statusCode = ((HttpWebResponse)webException.Response).StatusCode;
if (statusCode == HttpStatusCode.BadRequest)
{
return authService.Redirect(FailedRedirectUrlFilter(this, session.ReferrerUrl.SetParam("f", "AccessTokenFailed")));
}
}
return authService.Redirect(FailedRedirectUrlFilter(this, session.ReferrerUrl.SetParam("f", "Unknown")));
}
示例3: 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);
}
示例4: GetUserAuth
public IUserAuth GetUserAuth(IAuthSession authSession, IAuthTokens tokens)
{
//if (!authSession.UserAuthId.IsNullOrEmpty())
//{
// var userAuth = GetUserAuth(authSession.UserAuthId);
// if (userAuth != null)
// {
// return userAuth;
// }
//}
if (!authSession.UserAuthName.IsNullOrEmpty())
{
var userAuth = GetUserAuthByUserName(authSession.UserAuthName);
if (userAuth != null)
{
return userAuth;
}
}
if (tokens == null || tokens.Provider.IsNullOrEmpty() || tokens.UserId.IsNullOrEmpty())
{
return null;
}
return null;
}
示例5: Init
protected IAuthTokens Init(IServiceBase authService, ref IAuthSession session, Authenticate request)
{
Logger.Debug("SamlAuthProvider::Init:ENTER");
if (this.CallbackUrl.IsNullOrEmpty())
{
this.CallbackUrl = authService.Request.AbsoluteUri;
Logger.Debug("CallbackUrl was null, setting to: {0}".Fmt(this.CallbackUrl));
}
if (session.ReferrerUrl.IsNullOrEmpty() && authService.Request != null && authService.Request.Verb == "POST")
{
session.ReferrerUrl = this.IdpInitiatedRedirect;
}
else {
session.ReferrerUrl = GetReferrerUrl(authService, session, request);
}
Logger.Debug("Session ReferrerUrl Set to: {0}".Fmt(session.ReferrerUrl));
var tokens = session.ProviderOAuthAccess.FirstOrDefault(x => x.Provider == this.Provider);
if (tokens == null)
{
Logger.Debug("Tokens were null, initializing");
session.ProviderOAuthAccess.Add(tokens = new AuthTokens { Provider = this.Provider });
}
Logger.Debug("Tokens contains");
Logger.Debug(tokens.ToJson());
Logger.Debug("SamlAuthProvider::Init:RETURN");
return tokens;
}
示例6: HasAllPermissions
public bool HasAllPermissions(IAuthSession session, IAuthRepository authRepo)
{
if (session == null)
return false;
return this.RequiredPermissions.All(x => session.HasPermission(x, authRepo));
}
示例7: 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");
}
示例8: HasAllPermissions
public bool HasAllPermissions(IAuthSession session)
{
if (session == null)
return false;
return this.RequiredPermissions.All(session.HasPermission);
}
示例9: SaveUserAuth
/// <summary>
/// Saves the Auth Tokens for this request. Called in OnAuthenticated().
/// Overrideable, the default behaviour is to call IUserAuthRepository.CreateOrMergeAuthSession().
/// </summary>
protected virtual void SaveUserAuth(IServiceBase authService, IAuthSession session, IUserAuthRepository authRepo, IOAuthTokens tokens)
{
if (authRepo == null) return;
if (tokens != null)
{
session.UserAuthId = authRepo.CreateOrMergeAuthSession(session, tokens);
}
authRepo.LoadUserAuth(session, tokens);
foreach (var oAuthToken in session.ProviderOAuthAccess)
{
var authProvider = AuthService.GetAuthProvider(oAuthToken.Provider);
if (authProvider == null) continue;
var userAuthProvider = authProvider as OAuthProvider;
if (userAuthProvider != null)
{
userAuthProvider.LoadUserOAuthProvider(session, oAuthToken);
}
}
authRepo.SaveUserAuth(session);
var httpRes = authService.RequestContext.Get<IHttpResponse>();
if (httpRes != null)
{
httpRes.Cookies.AddPermanentCookie(HttpHeaders.XUserAuthId, session.UserAuthId);
}
OnSaveUserAuth(authService, session);
}
示例10: LoadUserAuth
public void LoadUserAuth(IAuthSession session, IAuthTokens tokens)
{
session.ThrowIfNull("session");
var userAuth = GetUserAuth(session, tokens);
LoadUserAuth(session, (UserAuth)userAuth);
}
示例11: CreateOrMergeAuthSession
public string CreateOrMergeAuthSession(IAuthSession authSession, IOAuthTokens tokens)
{
using (var redis = factory.GetClient())
{
UserOAuthProvider oAuthProvider = null;
var oAuthProviderId = GetAuthProviderByUserId(redis, tokens.Provider, tokens.UserId);
if (!oAuthProviderId.IsNullOrEmpty())
oAuthProvider = redis.As<UserOAuthProvider>().GetById(oAuthProviderId);
var userAuth = GetUserAuth(redis, authSession, tokens)
?? new UserAuth { Id = redis.As<UserAuth>().GetNextSequence(), };
if (oAuthProvider == null)
{
oAuthProvider = new UserOAuthProvider {
Id = redis.As<UserOAuthProvider>().GetNextSequence(),
UserAuthId = userAuth.Id,
Provider = tokens.Provider,
UserId = tokens.UserId,
};
var idx = IndexProviderToUserIdHash(tokens.Provider);
redis.SetEntryInHash(idx, tokens.UserId, oAuthProvider.Id.ToString());
}
oAuthProvider.PopulateMissing(tokens);
userAuth.PopulateMissing(oAuthProvider);
redis.Store(userAuth);
redis.Store(oAuthProvider);
redis.AddItemToSet(IndexUserAuthAndProviderIdsSet(userAuth.Id), oAuthProvider.Id.ToString());
return userAuth.Id.ToString();
}
}
示例12: OnAuthenticated
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
session.UserName = session.UserAuthName;
//Important: You need to save the session!
authService.SaveSession(session, SessionExpiry);
}
示例13: CreateOrMergeAuthSession
public string CreateOrMergeAuthSession(IAuthSession authSession, IOAuthTokens tokens)
{
var userAuth = GetUserAuth(authSession, tokens) ?? new UserAuth();
return dbFactory.Exec(dbCmd => {
var oAuthProvider = dbCmd.FirstOrDefault<UserOAuthProvider>(
"Provider = {0} AND UserId = {1}", tokens.Provider, tokens.UserId);
if (oAuthProvider == null)
{
oAuthProvider = new UserOAuthProvider {
Provider = tokens.Provider,
UserId = tokens.UserId,
};
}
oAuthProvider.PopulateMissing(tokens);
userAuth.PopulateMissing(oAuthProvider);
dbCmd.Save(userAuth);
oAuthProvider.UserAuthId = userAuth.Id != default(int)
? userAuth.Id
: (int) dbCmd.GetLastInsertId();
dbCmd.Save(oAuthProvider);
return oAuthProvider.UserAuthId.ToString();
});
}
示例14: HasAllRoles
public bool HasAllRoles(IAuthSession session, IAuthRepository authRepo)
{
if (session == null)
return false;
return this.RequiredRoles.All(x => session.HasRole(x, authRepo));
}
示例15: GetUserAuth
public UserAuth GetUserAuth(IAuthSession authSession, IOAuthTokens tokens)
{
if (!authSession.UserAuthId.IsNullOrEmpty())
{
var userAuth = GetUserAuth(authSession.UserAuthId);
if (userAuth != null) return userAuth;
}
if (!authSession.UserAuthName.IsNullOrEmpty())
{
var userAuth = GetUserAuthByUserName(authSession.UserAuthName);
if (userAuth != null) return userAuth;
}
if (tokens == null || tokens.Provider.IsNullOrEmpty() || tokens.UserId.IsNullOrEmpty())
return null;
var oAuthProvider = Session.QueryOver<UserOAuthProviderPersistenceDto>()
.Where(x => x.Provider == tokens.Provider)
.And(x => x.UserId == tokens.UserId)
.SingleOrDefault();
if (oAuthProvider != null)
{
return Session.QueryOver<UserAuthPersistenceDto>()
.Where(x => x.Id == oAuthProvider.UserAuthId)
.SingleOrDefault();
}
return null;
}