本文整理汇总了C#中IAuthSession.TranslateTo方法的典型用法代码示例。如果您正苦于以下问题:C# IAuthSession.TranslateTo方法的具体用法?C# IAuthSession.TranslateTo怎么用?C# IAuthSession.TranslateTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAuthSession
的用法示例。
在下文中一共展示了IAuthSession.TranslateTo方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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
}
示例2: SaveUserAuth
public void SaveUserAuth(IAuthSession authSession)
{
dbFactory.Exec(dbCmd => {
var userAuth = !authSession.UserAuthId.IsNullOrEmpty()
? dbCmd.GetByIdOrDefault<UserAuth>(authSession.UserAuthId)
: authSession.TranslateTo<UserAuth>();
if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty())
userAuth.Id = int.Parse(authSession.UserAuthId);
userAuth.ModifiedDate = DateTime.UtcNow;
if (userAuth.CreatedDate == default(DateTime))
userAuth.CreatedDate = userAuth.ModifiedDate;
dbCmd.Save(userAuth);
});
}
示例3: SaveUserAuth
public void SaveUserAuth(IAuthSession authSession)
{
using (var session = _documentStore.OpenSession())
{
int idInt = int.Parse(authSession.UserAuthId);
var userAuth = !authSession.UserAuthId.IsNullOrEmpty()
? session.Load<UserAuth>(idInt)
: authSession.TranslateTo<UserAuth>();
if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty())
userAuth.Id = idInt;
userAuth.ModifiedDate = DateTime.UtcNow;
if (userAuth.CreatedDate == default(DateTime))
userAuth.CreatedDate = userAuth.ModifiedDate;
session.Store(userAuth);
session.SaveChanges();
}
}
示例4: SaveUserAuth
public void SaveUserAuth(IAuthSession authSession)
{
var userAuth = !authSession.UserAuthId.IsNullOrEmpty()
? GetUserAuth(authSession.UserAuthId)
: authSession.TranslateTo<UserAuth>();
if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty())
userAuth.Id = int.Parse(authSession.UserAuthId);
userAuth.ModifiedDate = DateTime.UtcNow;
if (userAuth.CreatedDate == default(DateTime))
userAuth.CreatedDate = userAuth.ModifiedDate;
var collection = mongoDatabase.GetCollection<UserAuth>("UserAuth");
collection.Insert(userAuth);
}
示例5: SaveUserAuth
public void SaveUserAuth(IAuthSession authSession)
{
using (var redis = factory.GetClient())
{
var userAuth = !authSession.UserAuthId.IsNullOrEmpty()
? GetUserAuth(redis, authSession.UserAuthId)
: authSession.TranslateTo<UserAuth>();
if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty())
userAuth.Id = int.Parse(authSession.UserAuthId);
userAuth.ModifiedDate = DateTime.UtcNow;
if (userAuth.CreatedDate == default(DateTime))
userAuth.CreatedDate = userAuth.ModifiedDate;
redis.Store(userAuth);
}
}
开发者ID:grammarware,项目名称:fodder,代码行数:18,代码来源:src_ServiceStack_ServiceInterface_Auth_RedisAuthRepository.cs
示例6: OnAuthenticated
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
base.OnAuthenticated(authService, session, tokens, authInfo);
CustomFoo = "SOMETHING CUSTOM";
//if (session.Email == null)
//{
// session.Email = session.Email ?? tokens.Email;
// session.UserAuthName = session.UserAuthName ?? session.Email;
// session.UserName = session.UserName ?? session.Email;
// using (var db = authService.TryResolve<IDbConnectionFactory>().Open())
// {
// int userAuthId = Convert.ToInt32(session.UserAuthId);
// var userAuth = db.Select<UserAuth>(ua => ua.Id == userAuthId).FirstOrDefault();
// if (userAuth != null && (userAuth.Email == null || userAuth.UserName == null))
// {
// userAuth.Email = userAuth.Email ?? session.Email;
// userAuth.UserName = userAuth.UserName ?? session.Email;
// db.Update<UserAuth>(userAuth);
// }
// }
//}
//else if (session.UserAuthName == null)
//{
// session.UserAuthName = session.Email;
//}
//session.Email = session.Email ?? tokens.Email;
//session.UserAuthName = session.UserAuthName ?? session.Email;
//session.UserName = session.UserName ?? session.Email;
//Populate all matching fields from this session to your own custom User table
var user = session.TranslateTo<User>();
//user.Id = int.Parse(session.UserAuthId);
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;
user.Email = authToken.Email;
user.FirstName = authToken.FirstName;
user.LastName = authToken.LastName;
}
//else if (authToken.Provider == TwitterAuthProvider.Name)
//{
// user.TwitterName = authToken.DisplayName;
//}
//else if (authToken.Provider == GoogleOpenIdOAuthProvider.Name)
//{
// user.GoogleUserId = authToken.UserId;
// user.GoogleFullName = authToken.FullName;
// user.GoogleEmail = authToken.Email;
//}
//else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
//{
// user.YahooUserId = authToken.UserId;
// user.YahooFullName = authToken.FullName;
// user.YahooEmail = authToken.Email;
//}
}
//if (AppHost.AppConfig.AdminUserNames.Contains(session.Email)
// && !session.HasRole(RoleNames.Admin))
//{
// using (var assignRoles = authService.ResolveService<AssignRolesService>())
// {
// assignRoles.Post(new AssignRoles
// {
// UserName = session.UserAuthName,
// Roles = { RoleNames.Admin }
// });
// }
//}
//Resolve the DbFactory from the IOC and persist the user info
//authService.TryResolve<IDbConnectionFactory>().Run(db => db.Save(user));
using (var db = authService.TryResolve<IDbConnectionFactory>().Open())
{
//Update (if exists) or insert populated data into 'User'
var existingUser = db.Select<User>(u => u.Email == user.Email).FirstOrDefault();
if (existingUser != null)
{
user.Id = existingUser.Id;
}
else
{
user.CreatedAt = DateTime.Now;
}
db.Save(user);
}
}
示例7: SaveUserAuth
public void SaveUserAuth(IAuthSession authSession)
{
using (var uow = GetUnitOfWork())
{
var userAuth = !authSession.UserAuthId.IsNullOrEmpty()
? uow.GetSession().Load<UserAuthPersistenceDto>(int.Parse(authSession.UserAuthId))
: authSession.TranslateTo<UserAuth>();
if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty())
userAuth.Id = int.Parse(authSession.UserAuthId);
userAuth.ModifiedDate = userAuth.ModifiedDate;
if (userAuth.CreatedDate == default(DateTime))
userAuth.CreatedDate = userAuth.ModifiedDate;
uow.Save(new UserAuthPersistenceDto(userAuth));
uow.CommitTransaction();
}
}
示例8: SaveUserAuth
public void SaveUserAuth(IAuthSession authSession)
{
var userAuth = !authSession.UserAuthId.IsNullOrEmpty()
? Session.Load<UserAuthPersistenceDto>(int.Parse(authSession.UserAuthId))
: authSession.TranslateTo<UserAuth>();
if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty())
userAuth.Id = int.Parse(authSession.UserAuthId);
userAuth.ModifiedDate = userAuth.ModifiedDate;
if (userAuth.CreatedDate == default(DateTime))
userAuth.CreatedDate = userAuth.ModifiedDate;
Session.Save(new UserAuthPersistenceDto(userAuth));
}
示例9: OnAuthenticated
public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
//Fill the IAuthSession with data which you want to retrieve in the app eg:
//Resolve the DbFactory from the IOC and persist the user info
var DbFactory = authService.TryResolve<IDbConnectionFactory>();
var user = session.TranslateTo<User>();
TimeSpan sessionExpiry = TimeSpan.FromDays(7 * 2); //2 weeks
user.UserAuthId = int.Parse(session.UserAuthId);
if (string.IsNullOrEmpty(user.UserName))
user.UserName = user.Id.ToString();
foreach (var authToken in session.ProviderOAuthAccess)
{
if (authToken.Provider == FacebookAuthProvider.Name)
{
user.UserName = "fb-" + authToken.UserName.Replace("_", "-");
user.DisplayName = authToken.DisplayName;
user.FacebookName = authToken.DisplayName;
user.FacebookFirstName = authToken.FirstName;
user.FacebookLastName = authToken.LastName;
user.FacebookEmail = authToken.Email;
user.ProfileUrl = "http://www.facebook.com/" + authToken.UserName;
user.AvatarUrl = "http://graph.facebook.com/" + authToken.UserName + "/picture";
}
else if (authToken.Provider == TwitterAuthProvider.Name)
{
user.UserName = "tw-" + authToken.UserName.Replace("_", "-");
user.TwitterName = authToken.DisplayName;
user.ProfileUrl = "https://twitter.com/" + authToken.UserName;
TwitterHelper twHelper = new TwitterHelper(authToken.AccessToken, authToken.AccessTokenSecret);
Twitterizer.TwitterUser twUser = twHelper.GetUser(authToken.UserName);
if (twUser != null)
{
user.AvatarUrl = twUser.ProfileImageLocation;
user.DisplayName = twUser.Name;
}
}
}
var DbUser = DbFactory.Run(repo => repo.Select<User>(u => u.UserName == user.UserName));
if (DbUser.Count == 0)
{
user.ApiKey = Guid.NewGuid().ToString().Replace("-", "");
if (string.IsNullOrEmpty(user.UserName))
user.UserName = session.UserAuthName;
if (string.IsNullOrEmpty(user.DisplayName))
user.DisplayName = session.DisplayName;
if (string.IsNullOrEmpty(user.AvatarUrl))
user.AvatarUrl = "/Content/Images/avatar.jpg";
if (string.IsNullOrEmpty(user.ProfileUrl))
user.ProfileUrl = "/Users/View/" + user.UserAuthId.ToString();
DbFactory.Run(repo => repo.Insert<User>(user));
}
else
{
var u = DbUser[0];
if (!string.IsNullOrEmpty(user.DisplayName))
u.DisplayName = user.DisplayName;
if (!string.IsNullOrEmpty(user.AvatarUrl))
u.AvatarUrl = user.AvatarUrl;
if (!string.IsNullOrEmpty(user.ProfileUrl))
u.ProfileUrl = user.ProfileUrl;
DbFactory.Run(repo => repo.Update<User>(u));
}
var userAuth = DbFactory.Run(repo => repo.Select<UserAuth>(u => u.Id == user.UserAuthId && u.UserName == null));
if (userAuth.Count > 0)
{
// A username is required in the UserAuth table so that a
// user can log in using the /auth/credentials endpoint:
userAuth[0].UserName = user.UserName;
DbFactory.Run(dbCmd => dbCmd.Save(userAuth[0]));
}
//Important: You need to save the session!
session.UserName = user.UserName;
authService.SaveSession(session, sessionExpiry);
}