本文整理汇总了C#中IAuthSession.HasRole方法的典型用法代码示例。如果您正苦于以下问题:C# IAuthSession.HasRole方法的具体用法?C# IAuthSession.HasRole怎么用?C# IAuthSession.HasRole使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAuthSession
的用法示例。
在下文中一共展示了IAuthSession.HasRole方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HasAllRoles
public bool HasAllRoles(IAuthSession session, IAuthRepository authRepo)
{
if (session == null)
return false;
return this.RequiredRoles.All(x => session.HasRole(x, authRepo));
}
示例2: OnAuthenticated
public override void OnAuthenticated(
IServiceBase authService,
IAuthSession session,
IOAuthTokens tokens,
Dictionary<string, string> authInfo)
{
base.OnAuthenticated(authService, session, tokens, authInfo);
//Populate all matching fields from this session to your own custom User table
var user = session.TranslateTo<User>();
user.Id = int.Parse(session.UserAuthId);
//user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty() ? CreateGravatarUrl(session.Email, 64) : null;
foreach (var authToken in session.ProviderOAuthAccess)
{
// //if (authToken.Provider == FacebookAuthProvider.Name)
// //{
// // user.FacebookName = authToken.DisplayName;
// // user.FacebookFirstName = authToken.FirstName;
// // user.FacebookLastName = authToken.LastName;
// // user.FacebookEmail = authToken.Email;
// //}
// //else
if (authToken.Provider == GoogleOpenIdOAuthProvider.Name)
{
user.GoogleUserId = authToken.UserId;
user.GoogleFullName = authToken.FullName;
user.GoogleEmail = authToken.Email;
if (user.Email == null) user.Email = user.GoogleEmail;
if (user.UserName == null) user.UserName = user.GoogleUserId;
}
else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
{
user.YahooUserId = authToken.UserId;
user.YahooFullName = authToken.FullName;
user.YahooEmail = authToken.Email;
}
}
var adminUserNames = AppHost.AppConfig.AdminUserNames;
if (AppHost.AppConfig.AdminUserNames.Contains(session.UserAuthName)
&& !session.HasRole(RoleNames.Admin))
{
using (var assignRoles = authService.ResolveService<AssignRolesService>())
{
assignRoles.Post(new AssignRoles
{
UserName = session.UserAuthName,
Roles = { RoleNames.Admin }
});
//authService.TryResolve<IDbConnectionFactory>().Run(db => db.Save(user));
}
}
//Resolve the DbFactory from the IOC and persist the user info
}
示例3: OnAuthenticated
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
{
base.OnAuthenticated(authService, session, tokens, authInfo);
//Populate all matching fields from this session to your own custom User table
var user = session.ConvertTo<User>();
user.Id = int.Parse(session.UserAuthId);
user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()
? CreateGravatarUrl(session.Email, 64)
: null;
foreach (var authToken in session.ProviderOAuthAccess)
{
if (authToken.Provider == FacebookAuthProvider.Name)
{
user.FacebookName = authToken.DisplayName;
user.FacebookFirstName = authToken.FirstName;
user.FacebookLastName = authToken.LastName;
user.FacebookEmail = authToken.Email;
}
else if (authToken.Provider == TwitterAuthProvider.Name)
{
user.TwitterName = user.DisplayName = authToken.UserName;
}
else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
{
user.YahooUserId = authToken.UserId;
user.YahooFullName = authToken.FullName;
user.YahooEmail = authToken.Email;
}
}
var userAuthRepo = authService.TryResolve<IAuthRepository>();
if (AppHost.AppConfig.AdminUserNames.Contains(session.UserAuthName)
&& !session.HasRole(RoleNames.Admin, userAuthRepo))
{
var userAuth = userAuthRepo.GetUserAuth(session, tokens);
userAuthRepo.AssignRoles(userAuth, roles: new[] { RoleNames.Admin });
}
//Resolve the DbFactory from the IOC and persist the user info
using (var db = authService.TryResolve<IDbConnectionFactory>().Open())
{
db.Save(user);
}
}
示例4: HasAllRoles
public bool HasAllRoles(IAuthSession session)
{
return this.RequiredRoles
.All(requiredRole => session != null
&& session.HasRole(requiredRole));
}
示例5: IsAuthorized
public bool IsAuthorized(Operation operation, IRequest req, IAuthSession session)
{
if (HostContext.HasValidAuthSecret(req))
return true;
if (operation.RequiresAuthentication && !session.IsAuthenticated)
return false;
var authRepo = HostContext.AppHost.GetAuthRepository(req);
using (authRepo as IDisposable)
{
if (!operation.RequiredRoles.IsEmpty() && !operation.RequiredRoles.All(x => session.HasRole(x, authRepo)))
return false;
if (!operation.RequiredPermissions.IsEmpty() && !operation.RequiredPermissions.All(x => session.HasPermission(x, authRepo)))
return false;
if (!operation.RequiresAnyRole.IsEmpty() && !operation.RequiresAnyRole.Any(x => session.HasRole(x, authRepo)))
return false;
if (!operation.RequiresAnyPermission.IsEmpty() && !operation.RequiresAnyPermission.Any(x => session.HasPermission(x, authRepo)))
return false;
return true;
}
}
示例6: HasAnyRoles
public virtual bool HasAnyRoles(IAuthSession session)
{
return this.RequiredRoles
.Any(requiredRole => session != null
&& session.HasRole(requiredRole));
}
示例7: HasAnyRoles
public virtual bool HasAnyRoles(IAuthSession session, IAuthRepository authRepo)
{
return this.RequiredRoles
.Any(requiredRole => session != null
&& session.HasRole(requiredRole, authRepo));
}