本文整理汇总了C#中UserManager.CreateIdentity方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.CreateIdentity方法的具体用法?C# UserManager.CreateIdentity怎么用?C# UserManager.CreateIdentity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.CreateIdentity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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");
}
}
示例2: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
// Default UserStore constructor uses the default connection string named: DefaultConnection
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
//IdentityResult IdUserResult;
var user = new IdentityUser() { UserName = UserName.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
// IdUserResult = manager.AddToRole(manager.FindByName(user.UserName).Id, "member");
Session["uPass"] = Password.Text;
Response.Redirect("~/WebForm2.aspx");
// StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName);
}
else
{
StatusMessage.Text = result.Errors.FirstOrDefault();
}
}
示例3: btnSave_Click
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
// Default UserStore constructor uses the default connection string named: DefaultConnection
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
var user = new IdentityUser() { UserName = txtUsername.Text };
IdentityResult result = manager.Create(user, txtPassword.Text);
if (result.Succeeded)
{
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
Response.Redirect("/user/index.aspx");
}
else
{
lblStatus.Text = result.Errors.FirstOrDefault();
}
}
catch (Exception d)
{
Response.Redirect("/error.aspx");
}
}
示例4: btnRegister_Click
protected void btnRegister_Click(object sender, EventArgs e)
{
try
{
// Default UserStore constructor uses the default connection string named: DefaultConnection
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
var user = new IdentityUser() { UserName = txtUsername.Text };
IdentityResult result = manager.Create(user, txtPassword.Text);
if (result.Succeeded)
{
//lblStatus.Text = string.Format("User {0} was created successfully!", user.UserName);
//lblStatus.CssClass = "label label-success";
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
Response.Redirect("admin/main-menu.aspx");
}
else
{
lblStatus.Text = result.Errors.FirstOrDefault();
lblStatus.CssClass = "label label-danger";
}
}
catch (Exception q)
{
Response.Redirect("/error.aspx");
}
}
示例5: 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;
}
}
示例6: SignIn
protected void SignIn(object sender, EventArgs e)
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
IdentityUser user = userManager.Find(tbUsername.Text, tbPassword.Text);
//if user info is found
if (user != null)
{
//create cookie
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
ClaimsIdentity userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
//sign in
authenticationManager.SignIn(new AuthenticationProperties {IsPersistent = false}, userIdentity);
var returnUrl = Request.QueryString["returnUrl"];
//if user came from different page, redirect to that one. Otherwise redirect to main page.
Response.Redirect(returnUrl ?? "~/default.aspx");
}
//if not, show error message.
else
{
lblConfirmationText.Text = "Invalid username or password.";
}
}
示例7: RegisterButton_Click
protected void RegisterButton_Click(object sender, EventArgs e)
{
//crete new userStore and userManager objects
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = new IdentityUser()
{
UserName = UserNameTextBox.Text,
PhoneNumber = PhoneNumberTextBox.Text,
Email = EmailTextBox.Text
};
//create new user in the dbb and store the resukt
IdentityResult result = userManager.Create(user, PasswordTextBox.Text);
//check if succesfully registered
if (result.Succeeded)
{
//authenticate and login new user
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);//store info in session
//sign in
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
//redirect to main menu
Response.Redirect("~/Secured/TodoList.aspx");
}
else
{
//display error in the AlertFlash div
StatusLabel.Text = result.Errors.FirstOrDefault();
AlertFlash.Visible = true;
}
}
示例8: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
// Default UserStore constructor uses the default connection string named: DefaultConnectionEF
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
var user = new IdentityUser() { UserName = txtUName.Text };
user.Email = txtEmail.Text;
user.PhoneNumber = txtPhone.Text;
IdentityResult result = manager.Create(user, txtPass.Text);
if (result.Succeeded)
{
lblStatus.Text = string.Format("User {0} was created successfully!", user.UserName);
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
Response.Redirect("/admin/main.aspx");
}
else
{
lblStatus.Text = result.Errors.FirstOrDefault();
}
}
示例9: btnLogin_Click
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
//store user information into variables
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(txtUsername.Text, txtPassword.Text);
//if there is a current user
if (user != null)
{
//if the user is authenticated, redirect to products page
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
Response.Redirect("/admin/products.aspx", false);
}
else //if any fields are blank
{
//show a message to the user
lblStatusMessage.Text = "Invalid username or password.";
lblStatusMessage.Visible = true;
}
}
catch (Exception)
{
Response.Redirect("/Error.aspx");
}
}
示例10: btnLogin_Click
protected void btnLogin_Click(object sender, EventArgs e)
{
var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
//database connection not-authicating
//System.Data.Entity.Core.EntityCommandExecutionException
//System.Data.SqlClient.SqlException
try
{
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/main.aspx");
}
else
{
lblStatus.Text = "Invalid username or password.";
}
}
catch (System.Data.Entity.Core.EntityCommandExecutionException ECEE) {
Server.Transfer("/ErrorPage.aspx", true);
}
catch (System.Data.SqlClient.SqlException SqlE) {
Server.Transfer("/ErrorPage.aspx", true);
}
}
示例11: Register
public ActionResult Register(User user)
{
string temp = user.Password;
var userStore = new UserStore<IdentityUser>();
var manager = new UserManager<IdentityUser>(userStore);
var user2 = new IdentityUser() { UserName = user.Username };
IdentityResult result = manager.Create(user2, user.Password);
if (result.Succeeded)
{
TempData["message"] = "Identity user create worked";
//var temp2 = this.ControllerContext.HttpContext;
var authenticationManager = HttpContext.GetOwinContext().Authentication;
//var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = manager.CreateIdentity(user2, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
//Response.Redirect("~/Login.aspx");
//return View("Login");
if (User.Identity.IsAuthenticated)
{
TempData["message"] += "/n User.Identity.IsAuthenticate working";
}
return View("Index");
}
else
{
TempData["message"] = "Failed: " + result.Errors.FirstOrDefault();
}
return View("Index");
}
示例12: 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");
}
}
示例13: GenerateUserIdentity
public ClaimsIdentity GenerateUserIdentity(UserManager<User> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
示例14: 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);
// Find the user
var user = userManager.Find(UserNameTextBox.Text, PasswordTextBox.Text);
// check if username and password combo exists
if (user != null)
{
// authenticate and login new user
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
// redirect to the Main Menu page
Response.Redirect("~/game.aspx");
}
else
{
StatusLabel.Text = "Invalid Username or Password";
AlertFlash.Visible = true;
}
}
示例15: 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");
//}
}