本文整理汇总了C#中ApplicationUserManager.FindByIdAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationUserManager.FindByIdAsync方法的具体用法?C# ApplicationUserManager.FindByIdAsync怎么用?C# ApplicationUserManager.FindByIdAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApplicationUserManager
的用法示例。
在下文中一共展示了ApplicationUserManager.FindByIdAsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetMedicationDetails
public async static Task<Tuple<List<Medication>, Hl7.Fhir.Model.Patient>> GetMedicationDetails(string id, ApplicationUserManager userManager)
{
Tuple<List<Medication>, Hl7.Fhir.Model.Patient> tup;
using (var dbcontext = new ApplicationDbContext())
{
// Should be FhirID
var user = await userManager.FindByIdAsync(id);
if (user == null)
{
return null;
}
var client = new FhirClient(Constants.HapiFhirServerBase);
if (string.IsNullOrWhiteSpace(user.FhirPatientId))
{
var result = client.Create(new Hl7.Fhir.Model.Patient() { });
user.FhirPatientId = result.Id;
await userManager.UpdateAsync(user);
}
var patient = client.Read<Hl7.Fhir.Model.Patient>(Constants.PatientBaseUrl + user.FhirPatientId);
tup= new Tuple<List<Medication>, Patient>(await EhrBase.GetMedicationDataForPatientAsync(user.FhirPatientId, client), patient);
return tup;
}
}
示例2: OnActionExecuting
internal void OnActionExecuting(
ActionExecutingContext filterContext,
ApplicationUserManager userManager,
ClientIdCalculator clientIdCalculator)
{
if (filterContext.ActionParameters.ContainsKey(USER_CONTEXT_KEY))
{
ApplicationUser applicationUser;
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
string userId = filterContext.HttpContext.User.Identity.GetUserId();
applicationUser = userManager.FindByIdAsync(userId).Result;
}else
{
applicationUser = new AnonymousApplicationUser();
if (this.RequiresGamingGroup)
{
filterContext.Result = new RedirectToRouteResult(MVC.Account.Login().GetRouteValueDictionary());
}
}
applicationUser.AnonymousClientId = clientIdCalculator.GetClientId(filterContext.HttpContext.Request, applicationUser);
filterContext.ActionParameters[USER_CONTEXT_KEY] = applicationUser;
}
base.OnActionExecuting(filterContext);
}
示例3: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
this.manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
this.currentUser = manager.FindByIdAsync(User.Identity.GetUserId()).Result;
if (this.currentUser == null)
{
Response.Redirect("/Account/Login");
}
this.firstName.Attributes.Add("placeholder", currentUser.FirstName);
this.lastName.Attributes.Add("placeholder", currentUser.LastName);
//this.firstName.Text = currentUser.FirstName;
//this.lastName.Text = currentUser.LastName;
}
示例4: Disconnect
public async Task<ActionResult> Disconnect(ApplicationSignInManager signInManager, ApplicationUserManager userManager, string provider, string userId, string redirectUri)
{
var userLoginInfo = (await userManager.GetLoginsAsync(userId)).First(x => x.LoginProvider == provider);
var result = await userManager.RemoveLoginAsync(userId, userLoginInfo);
if (result.Succeeded)
{
var user = await userManager.FindByIdAsync(userId);
if (user != null)
{
await signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
}
}
else
{
Logger.Error($"Unable to disconnect {provider} from user {userId}");
}
return new RedirectResult(redirectUri);
}
示例5: InsertOrUpdate
public async Task<bool> InsertOrUpdate(IdentityUserViewModel model, ApplicationUserManager userManager)
{
var user = await userManager.FindByIdAsync(model.Id);
if (user == null)
{
user = new ApplicationUser();
user.Assign(model);
var result = await userManager.CreateAsync(user, "1234567");
if (!result.Succeeded) return false;
model.Id = user.Id;
}
else
{
user.Email = model.Email;
user.UserName = model.UserName;
user.DUI = model.DUI;
user.PhoneNumber = model.PHONE_2;
user.ADDRESS = model.ADDRESS;
user.Category = model.Category;
user.FirstName = model.FirstName;
user.LastName = model.LastName;
user.DUI = model.DUI;
user.PHONE_2 = model.PHONE_2;
user.ProfilePicture = model.ProfilePicture;
user.CenterId = model.CenterId;
//user.Address = model.Address;
//user.FirstName = model.FirstName;
//user.LastName = model.LastName;
//user.DocumentNum = model.DocumentNum;
//user.ProfilePicture = model.ProfilePicture;
await userManager.UpdateAsync(user);
}
if (model.ForceChangePassword)
{
var tok = await userManager.GeneratePasswordResetTokenAsync(model.Id);
var result = await userManager.ResetPasswordAsync(model.Id, tok, model.Password);
if (!result.Succeeded) return false;
}
var roles = await userManager.GetRolesAsync(model.Id);
if (!roles.Any() && !string.IsNullOrEmpty(model.Role))
{
var res = await userManager.AddToRoleAsync(model.Id, model.Role);
}
if (roles.All(r => r != model.Role) && roles.Any())
{
var result = await userManager.AddToRoleAsync(model.Id, model.Role);
}
roles.Where(r => r != model.Role).ToList().ForEach(role => userManager.RemoveFromRole(model.Id, role));
if (model.CenterId != 0)
{
var claim = await Context.UserClaims.FirstOrDefaultAsync(c => c.UserId == user.Id && c.ClaimType == "CenterId");
var claims = await userManager.GetClaimsAsync(user.Id);
if (claim != null)
{
claim.ClaimValue = model.CenterId.Value.ToString();
await Context.SaveChangesAsync();
}
else
{
await userManager.AddClaimAsync(model.Id, new System.Security.Claims.Claim("CenterId", model.CenterId.ToString()));
}
}
return true;
}
示例6: UpdateRoles
public async Task<IdentityResult> UpdateRoles(JObject roles)
{
string userId = ((dynamic)roles).Id.Value;
string roleId = ((dynamic)roles).RoleId.Value;
string roleName = ((dynamic)roles).RoleName.Value;
IdentityResult result = null;
#region TODO: Remove unused commented code
//IdentityRole role = (from r in _contextProvider.Context.Roles where r.Id == roleId select r).FirstOrDefault();
//var user = _contextProvider.Context.Users.Where(x => x.Id == userId).FirstOrDefault();
//if (user.Roles.Any(x => x.RoleId == roleId))
//{
// // User is in the Admin Role
//}
//else
//{
// //User is not in the Admin Role
//}
//var role = (from r in _contextProvider.Context.Roles where r.Id == roleId select r).FirstOrDefault();
//var users = _contextProvider.Context.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(role.Id)).ToList();
//if (users.Find(x => x.Id == userId) != null)
//{
// // User is in the Admin Role
//}
//else
//{
// //User is not in the Admin Role
//}
#endregion
var userStore = new UserStore<ApplicationUser>(_contextProvider.Context);
var userManager = new ApplicationUserManager(userStore);
var roleStore = new RoleStore<IdentityRole>(_contextProvider.Context);
var roleManager = new ApplicationRoleManager(roleStore);
ApplicationUser user = await userManager.FindByIdAsync(userId);//.ConfigureAwait(false);
IList<string> role = new List<string>();
role.Add(roleName);
if (user.Roles.Any(x => x.RoleId == roleId))
{
//remove user and roll mapping
result = await userManager.RemoveUserFromRolesAsync(userId, role).ConfigureAwait(false);
result = await userManager.RemoveClaimAsync(userId, new Claim(IdentityConstants.ClaimTypes.Role, roleName));
}
else
{
result = await userManager.AddUserToRolesAsync(userId, role).ConfigureAwait(false);
result = await userManager.AddClaimAsync(userId, new Claim(IdentityConstants.ClaimTypes.Role, roleName));
//User is not in the Admin Role
}
#region TODO: Remove unused commented code
//if (user.Roles.Count > 0)
//{
// var roleExists = user.Roles.First(x => x.RoleId == roleId);
// if (roleExists != null)
// {
// //remove user and roll mapping
// //result = await userManager.RemoveFromRoleAsync(userId, roleId).ConfigureAwait(false);
// //remove user and its claim
// //Claim c = new Claim();
// //c.Type = "role";
// //c.Value
// //result = await userManager.RemoveClaimAsync(userId,new Claim() { })
// }
// else
// {
// //result = await userManager.AddToRoleAsync(userId,roleId);
// }
//}
#endregion
return await userManager.UpdateAsync(user).ConfigureAwait(false);
}
示例7: DeleteUser
public async Task<ActionResult> DeleteUser(string Username)
{
try
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
ApplicationUser user = context.Users.Where(u => u.UserName.Equals(Username, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
string userID = user.Id;
if (ModelState.IsValid)
{
if (userID == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var userDel = await manager.FindByIdAsync(userID);
var logins = user.Logins;
foreach (var login in logins.ToList())
{
await manager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
}
var rolesForUser = await manager.GetRolesAsync(userID);
if (rolesForUser.Count() > 0)
{
foreach (var item in rolesForUser.ToList())
{
// item should be the name of the role
var result = await manager.RemoveFromRoleAsync(user.Id, item);
}
}
await manager.DeleteAsync(user);
TempData["ValidationMessage"] = ("Success: " + " " + Username + " " + "Was Removed");
return View("ManageUser");
}
else
{
TempData["ValidationMessage"] = ("Error: " + " " + Username + " " + "Was Not Removed");
return View("ManageUser");
}
}
catch
{
TempData["ValidationMessage"] = ("Error: Something Went Wrong");
return View("ManageUser");
}
}