Since my app accepts email messges, and creates records for the users, it rather important they exist in the system.
So the background Windows service uses LinqToSql to update the tables.
Each user in Lightswitch is in the aspnet_table, there is a record for each application sharing the same database. So if your creating a user, you also need to know which application guid you need to connect the user too.
public int CreateUser(string UserName)
{
User myUser = new User();
myUser.Name = UserName;
db.Users.InsertOnSubmit(myUser);
db.SubmitChanges();
return (myUser.Id);
}
public Guid CreateAspUser(string UserName)
{
aspnet_User myUser = new aspnet_User();
myUser.ApplicationId = MyAppId;
myUser.UserId = Guid.NewGuid();
myUser.UserName = UserName;
myUser.LoweredUserName = UserName.ToLower();
myUser.IsAnonymous = false;
myUser.LastActivityDate = System.DateTime.Now;
db.aspnet_Users.InsertOnSubmit(myUser);
db.SubmitChanges();
return (myUser.UserId);
}
public Guid GetMyAppId(string appName)
{
var query = from aApp in db.aspnet_Applications where aApp.ApplicationName == appName select aApp;
return (query.First().ApplicationId);
}
public Guid GetUserID(String UserName)
{
var query = from aUser in db.aspnet_Users where aUser.UserName == UserName where aUser.ApplicationId == MyAppId select aUser;
if (query.Count<aspnet_User>() == 0)
{
return (Guid.Empty);
}
return (query.First().UserId);
}
public int GetPortalUserID(String UserName)
{
var query = from aUser in db.Users where aUser.Name == UserName select aUser;
if (query.Count<User>() == 0)
{
return (0);
}
return (query.First().Id);
}
So then it just a matter of calling the code:
Guid theUserID = GetUserID(UserName);
if (theUserID == Guid.Empty)
{ // No Such User
theUserID = CreateAspUser(UserName);
}
int thePortalID = GetPortalUserID(UserName);
if (thePortalID == 0)
{
// Need to create local user
thePortalID = CreateUser(UserName);
}
CreateAppointment(theUserID, thePortalID, Email.Subject, theMessage);
No comments:
Post a Comment