本文整理汇总了C#中UserManager.FindByNameAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.FindByNameAsync方法的具体用法?C# UserManager.FindByNameAsync怎么用?C# UserManager.FindByNameAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.FindByNameAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Invoke
public async Task Invoke(HttpContext httpContext, UserManager<ApplicationUser> manager)
{
if (httpContext.Request.Path.StartsWithSegments(_options.Path))
{
var headers = httpContext.Request.Headers;
if (!(headers.ContainsKey("ApiUser") && headers.ContainsKey("ApiToken")))
{
await httpContext.Authentication.ChallengeAsync();
return;
}
var apiUser = headers.FirstOrDefault(h => h.Key == "ApiUser").Value;
var token = headers.FirstOrDefault(h => h.Key == "ApiToken").Value;
var user = await manager.FindByNameAsync(apiUser).ConfigureAwait(false);
var authorized = await manager.VerifyUserTokenAsync(user, "Default", "api-request-injest", token).ConfigureAwait(false);
if (!authorized)
{
await httpContext.Authentication.ChallengeAsync();
return;
}
}
await _next(httpContext);
}
示例2: submitCodePost
public string submitCodePost(string title, string content, string type)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
CodePost tmp = new CodePost();
content = HttpUtility.JavaScriptStringEncode(content);
var store = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(store);
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
var currentUser = userManager.FindById(User.Identity.GetUserId());
if (title != null && content != null && type != null)
{
tmp.title = title;
tmp.content = content;
tmp.votes = 0;
tmp.user_id = user.Id;
tmp.userName = user.user;
tmp.userImgPath = currentUser.userImgPath;
}
bool result = updateHashTags(tmp);
if(result == false)
db.posts.Add(tmp);
db.SaveChanges();
return "success";
}
}
示例3: getAppUser
public ApplicationUser getAppUser(IPrincipal User)
{
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var userManager = new UserManager<ApplicationUser>(store);
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
return user;
}
示例4: GetUserId
private string GetUserId(string userName)
{
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var userManager = new UserManager<ApplicationUser>(store);
ApplicationUser user = userManager.FindByNameAsync(userName).Result;
return user.Id;
}
示例5: UpdateRuoloUtente
public async Task<IHttpActionResult> UpdateRuoloUtente(UpdateRuoloUtenteModel Model)
{
if (Model == null || !ModelState.IsValid)
return BadRequest(ModelState);
using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
var utente = await userManager.FindByNameAsync(Model.Username);
if (utente == null)
return NotFound();
if (Model.NuovoStato)
{
await userManager.AddToRoleAsync(utente.Id, Model.Ruolo);
}
else
{
await userManager.RemoveFromRoleAsync(utente.Id, Model.Ruolo);
}
}
return Ok();
}
示例6: GetUserByName
public async Task<MyUser> GetUserByName(string name)
{
var db = new DataContext();
var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
var currentUser = await userMan.FindByNameAsync(name);
return currentUser;
}
示例7: SetDisplayName
public static async Task SetDisplayName(this Controller baseController, ClaimsPrincipal user, UserManager<User> userManager)
{
if (baseController.TempData["UserDisplayName"] == null || string.IsNullOrEmpty(baseController.TempData["UserDisplayName"].ToString()))
{
if (user.Identity.IsAuthenticated)
{
var loggedInUser = await userManager.FindByNameAsync(user.Identity.Name);
baseController.TempData["UserDisplayName"] = loggedInUser.GetFirstName();
}
}
}
示例8: Index
public ActionResult Index()
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
model = new ImageModel();
if (user != null)
{
model.ImageUrl = user.ImageUrl;
}
return PartialView("~/Views/Shared/_LoginPartial.cshtml", model);
}
示例9: AddToCart
public ActionResult AddToCart(Product product)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
List<Product> products = new List<Product>();
user.UserCart.Products.Add(product);
foreach (var item in user.UserCart.Products)
{
products.Add(item);
}
return View(products);
}
示例10: GetCalcRelease
// GET: api/CalcReleases
public IQueryable<CalcRelease> GetCalcRelease()
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
string[] myInClause = null;
if (user.Scheme != null)
{
myInClause = user.Scheme.Split(',');
return db.CalcRelease.Where(s => myInClause.Contains(s.Scheme));
}
else
{
return null;
}
}
示例11: Create
// GET: Files/Create
public ActionResult Create()
{
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var userManager = new UserManager<ApplicationUser>(store);
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
if (user == null)
return View();
ViewBag.OwnerId = new SelectList(db.Users.Where(u => u.Id == user.Id), "Id", "FirstName");
ViewBag.ReceiverId = AddFirstItem(new SelectList(db.Users, "Id", "FirstName"));
ViewBag.GroupId = AddFirstItem(new SelectList(db.Groups, "GroupID", "GroupName"));
return View();
}
示例12: Get
public IQueryable<string> Get()
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
string[] myInClause = null;
var response = Request.CreateResponse();
if (user.Scheme != null)
{
myInClause = user.Scheme.Split(',');
return db.Schemes.Where(s => myInClause.Contains(s.Name)).Select(m => m.Name).Distinct();
}
else
{
return null;
}
}
示例13: Invoke
public async Task Invoke(HttpContext context, SignInManager<ApplicationUser> signInManager, UserManager<ApplicationUser> userManager)
{
if (context.User.Identity.IsAuthenticated)
{
this.SetNTContext(context);
}
// automatically logging in in the dev mode
else if (SiteSettings.IsEnvironment(SecurityConstants.DevEnvironment))
{
ApplicationUser user = await userManager.FindByNameAsync(SecuritySettings.NootusProfileUserName);
await signInManager.SignInAsync(user, false);
}
await this.next(context);
}
示例14: OnAuthenticated
/// <summary>
/// Invoked after the LTI request has been authenticated so the application can sign in the application user.
/// </summary>
/// <param name="context">Contains information about the login session as well as the LTI request.</param>
/// <param name="claims">Optional set of claims to add to the identity.</param>
/// <returns>A <see cref="Task"/> representing the completed operation.</returns>
public static async Task OnAuthenticated(LtiAuthenticatedContext context, IEnumerable<Claim> claims = null)
{
// Find existing pairing between LTI user and application user
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new LtiDb()));
var loginProvider = string.Join(":", new[] { context.Options.AuthenticationType, context.LtiRequest.ConsumerKey });
var providerKey = context.LtiRequest.UserId;
var login = new UserLoginInfo(loginProvider, providerKey);
var user = await userManager.FindAsync(login);
if (user == null)
{
var usernameContext = new LtiGenerateUserNameContext(context.OwinContext, context.LtiRequest);
await context.Options.Provider.GenerateUserName(usernameContext);
if (string.IsNullOrEmpty(usernameContext.UserName))
{
return;
}
user = await userManager.FindByNameAsync(usernameContext.UserName);
if (user == null)
{
user = new ApplicationUser { UserName = usernameContext.UserName };
var result = await userManager.CreateAsync(user);
if (!result.Succeeded)
{
return;
}
}
// Save the pairing between LTI user and application user
await userManager.AddLoginAsync(user.Id, login);
}
// Create the application identity, add the LTI request as a claim, and sign in
var identity = await userManager.CreateIdentityAsync(user, context.Options.SignInAsAuthenticationType);
identity.AddClaim(new Claim(context.Options.ClaimType, JsonConvert.SerializeObject(context.LtiRequest, Formatting.None,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), ClaimValueTypes.String, context.Options.AuthenticationType));
if (claims != null)
{
foreach (var claim in claims)
{
identity.AddClaim(claim);
}
}
context.OwinContext.Authentication.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);
// Redirect to original URL so the new identity takes affect
context.RedirectUrl = context.LtiRequest.Url.ToString();
}
示例15: Save
public async Task<WikiDownUser> Save(IPrincipal principal, UserManager<WikiDownUser> userManager)
{
var user = await userManager.FindByNameAsync(this.UserName);
var roles = this.GetRoles(principal, user);
if (user != null)
{
if (user.UserName == principal.Identity.Name)
{
var userAccessLevel = ArticleAccessHelper.GetAccessLevel(user.Roles);
if (userAccessLevel < ArticleAccessLevel.Admin)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
}
user.SetRoles(roles);
user.SetDisplayName(this.DisplayName);
user.SetEmail(this.Email);
if (!string.IsNullOrWhiteSpace(this.Password))
{
await userManager.RemovePasswordAsync(user.Id);
await userManager.AddPasswordAsync(user.Id, this.Password);
}
await userManager.UpdateAsync(user);
WikiDownUserCacheHelper.Clear(user.UserName);
}
else
{
user = new WikiDownUser(this.UserName) { Roles = roles };
user.SetDisplayName(this.DisplayName);
user.SetEmail(this.Email);
await userManager.CreateAsync(user, this.Password);
}
return user;
}