本文整理汇总了C#中UserManager.Find方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.Find方法的具体用法?C# UserManager.Find怎么用?C# UserManager.Find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.Find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetupRolesAndUsers
private static void SetupRolesAndUsers(DbContext context)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
// add roles
if (!roleManager.RoleExists(Role.Guest.ToString()))
roleManager.Create(new IdentityRole(Role.Guest.ToString()));
if (!roleManager.RoleExists(Role.Supplier.ToString()))
roleManager.Create(new IdentityRole(Role.Supplier.ToString()));
if (!roleManager.RoleExists(Role.Deactivated.ToString()))
roleManager.Create(new IdentityRole(Role.Deactivated.ToString()));
if (!roleManager.RoleExists(Role.User.ToString()))
roleManager.Create(new IdentityRole(Role.User.ToString()));
var adminRole = roleManager.FindByName(Role.Admin.ToString());
if (adminRole == null)
{
adminRole = new IdentityRole(Role.Admin.ToString());
roleManager.Create(adminRole);
}
#if DEBUG
//add admin user
var admin = userManager.Find(Admin_User, Admin_Pass);
if (admin == null)
{
admin = new ApplicationUser
{
UserName = Admin_User,
Email = Admin_Mail,
EmailConfirmed = true
};
var result = userManager.Create(admin, Admin_Pass);
// TODO: verify returned IdentityResult
userManager.AddToRole(admin.Id, Role.Admin.ToString());
result = userManager.SetLockoutEnabled(admin.Id, false);
}
var rolesForUser = userManager.GetRoles(admin.Id);
if (!rolesForUser.Contains(adminRole.Name))
{
var result = userManager.AddToRole(admin.Id, adminRole.Name);
}
//add normal user
if (userManager.Find("[email protected]", "1q2w3e4r") == null)
{
var user = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
EmailConfirmed = true
};
userManager.Create(user, "1q2w3e4r");
// TODO: verify returned IdentityResult
userManager.AddToRole(user.Id, Role.User.ToString());
}
#endif
}
示例2: LogIn
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = new UserManager();
var returnUrl = Request.QueryString["ReturnUrl"];
ApplicationUser user = manager.Find(UserName.Text, Password.Text);
if (user != null)
{
IdentityHelper.SignIn(manager, user, RememberMe.Checked);
if (returnUrl == null)
{
IdentityHelper.RedirectToReturnUrl("~/Game/User-Home.aspx", Response);
}
else
{
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
}
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
}
示例3: btnSignIn_OnClick
protected void btnSignIn_OnClick(object sender, EventArgs e)
{
UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
userStore.Context.Database.Connection.ConnectionString =
System.Configuration.ConfigurationManager.
ConnectionStrings["GarageDBConnectionString"].ConnectionString;
UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
//to retrieve a user from the database
var user = manager.Find(txtUserName.Text, txtPassword.Text);
if (user != null)
{
//Call OWIN functionality
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
//Sign in user
authenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = false
}, userIdentity);
//Redirect user to homepage
Response.Redirect("~/Index.aspx");
}
else
{
litStatus.Text = "Invalid username or password";
}
}
示例4: btnLogin_Click
protected void btnLogin_Click(object sender, EventArgs e)
{
UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
userStore.Context.Database.Connection.ConnectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["CANDZOILPDBConnectionString"].ConnectionString;
UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
var user = manager.Find(txtUserName.Text, txtPassword.Text);
if (user != null) {
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties{
IsPersistent = false
}, userIdentity);
Response.Redirect("~/Index.aspx");
}else{
litStatus.Text = "Invalid username or password";
}
}
示例5: SignIn
protected void SignIn(object sender, EventArgs e)
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(UserName.Text, Password.Text);
try
{
if (user != null)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
Response.Redirect("admin/main-menu.aspx");
}
else
{
StatusText.Text = "Invalid username or password.";
LoginStatus.Visible = true;
}
}
catch (Exception)
{
Server.Transfer("/error.aspx");
}
}
示例6: Validate
public override void Validate(string userNameOrEmail, string password)
{
try
{
using (var context = new IdentityDbContext())
{
using (var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(context)))
{
string userName = userNameOrEmail;
if (userNameOrEmail.Contains('@'))
{
var userForEmail = userManager.FindByEmail(userNameOrEmail);
if (userForEmail != null)
{
userName = userForEmail.UserName;
}
}
var user = userManager.Find(userName, password);
if (user == null)
{
var msg = String.Format("Unknown Username {0} or incorrect password {1}", userNameOrEmail, password);
Trace.TraceWarning(msg);
throw new FaultException(msg);
}
}
}
}
catch (Exception e)
{
var msg = e.Message;
Trace.TraceWarning(msg);
throw new FaultException(msg);
}
}
示例7: AuthModule
public AuthModule()
: base("/api")
{
Post["/authenticate"] = x => {
var bind = this.Bind<LoginRequest>();
UserManager<User> manager = new UserManager<User>(new UserStore());
var user = manager.Find(bind.Username, bind.Password);
if (user == null)
{
return new Response
{
StatusCode = HttpStatusCode.Unauthorized
};
}
else
{
var response = new Response
{
StatusCode = HttpStatusCode.OK
};
return response.WithCookie("sq-valid", user.UserName, DateTime.Now.AddMinutes(5));
}
};
Get["/logout"] = x => {
var response = new Response
{
StatusCode = HttpStatusCode.OK
};
return response.WithCookie("sq-valid", null, DateTime.Now.AddYears(-5));
};
}
示例8: btnSave_Click
protected void btnSave_Click(object sender, EventArgs e)
{
//try
//{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(txtUserName.Text, txtPassword.Text);
if (user != null)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
Response.Redirect("~/admin/MainMenu.aspx");
}
else
{
lblStatus.Text = "Invalid username or password.";
}
//}
//catch (System.Exception)
//{
// Response.Redirect("/MainMenu.aspx");
//}
}
示例9: btnLogin_Click
/**
Login - authenticate entered user credientials.
**/
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(txtUsername.Text, txtPassword.Text);
if (user != null)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
Response.Redirect("admin/bibleMenu.aspx");
}
else
{
lblStatus.Text = "Invalid username or password.";
}
}
catch (Exception ex)
{
Response.Redirect("/errors.aspx");
}
}
示例10: LoginButton_Click
protected void LoginButton_Click(object sender, EventArgs e)
{
// create new userStore and userManager objects
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
// search for and create a new user object
var user = userManager.Find(UserNameTextBox.Text, PasswordTextBox.Text);
// if a match is found for the user
if(user != null)
{
// authenticate and login our new user
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
// Sign the user
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
// Redirect to Main Menu
Response.Redirect("~/Contoso/MainMenu.aspx");
}
else
{
// throw an error to the AlertFlash div
StatusLabel.Text = "Invalid Username or Password";
AlertFlash.Visible = true;
}
}
示例11: LogIn
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = new UserManager();
ApplicationUser user = manager.Find(UserName.Text, Password.Text);
if (user != null)
{
IdentityHelper.SignIn(manager, user, RememberMe.Checked);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["J1500ConnectionString"].ConnectionString);
conn.Open();
SqlCommand cmd3 = new SqlCommand("Update AspNetUsers set LastLogin=GETDATE() where id='" + user.Id + "'", conn);
cmd3.ExecuteNonQuery();
conn.Close();
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
}
示例12: LogIn
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = new UserManager();
ApplicationUser user = manager.Find(UserName.Text, Password.Text);
if (user != null)
{
IdentityHelper.SignIn(manager, user, RememberMe.Checked);
// call db to get number of saved cars for logged in user
userName = UserName.Text;
if (!userName.Equals(""))
{
Application["userName"] = userName;
loadNextPage(userName);
}
}
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
}
示例13: btnLogin_Click
protected void btnLogin_Click(object sender, EventArgs e)
{
//declare the collection of users
UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
//declare the user manager
UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
//try to find the user
IdentityUser user = manager.Find(txtEmpNum.Text, txtPassword.Text);
if (user == null)
lblStatus.Text = "Username or Password is incorrect";
else
{
if (txtEmpNum.Text == "Administrator")
{
IdentityResult userResult = manager.AddToRole(user.Id, "Admin");
}
//add user to role
//authenticate user
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(userIdentity);
Response.Redirect("~/MainPage.aspx");
}
}
示例14: GetProfileDetail
//public OrderDetail UpdateOrderDetail(string id,DateTime deliveryTime, int )
//{
// mergedEntities db = new mergedEntities();
// OrderDetail orderDetail = db.OrderDetails.Where(o => o.Id == id)
// .FirstOrDefault();
// orderDetail.orderNumber = productID;
// orderDetail.deliveryTime = deliveryTime;
// db.SaveChanges();
// return orderDetail;
//}
//Get profile details
// change public AspNetUser to public RegisteredUser
public RegisteredUser GetProfileDetail(Login login)
{
UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
IdentityUser identityUser = manager.Find(login.UserName, login.Password);
mergedEntities db = new mergedEntities();
RegisteredUser USER = new RegisteredUser();
var query =
from a in db.AspNetUsers
where (a.Id == identityUser.Id)
select new
{
ID = a.Id,
UserName = a.UserName,
PhoneNumber = a.PhoneNumber,
Email = a.Email,
};
foreach (var item in query)
{
USER.Id = item.ID;
USER.UserName = item.UserName;
USER.TelNumber = item.PhoneNumber;
USER.Email = item.Email;
}
return USER;
}
示例15: Index
public ActionResult Index(Login login)
{
// UserStore and UserManager manages data retreival.
UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();
UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
IdentityUser identityUser = manager.Find(login.UserName,
login.Password);
if (ModelState.IsValid)
{
if (ValidLogin(login))
{
IAuthenticationManager authenticationManager
= HttpContext.GetOwinContext().Authentication;
authenticationManager
.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, login.UserName),
},
DefaultAuthenticationTypes.ApplicationCookie,
ClaimTypes.Name, ClaimTypes.Role);
// SignIn() accepts ClaimsIdentity and issues logged in cookie.
authenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = false
}, identity);
return RedirectToAction("SecureArea", "Home");
}
}
return View();
}