本文整理汇总了C#中AuthenticationIdentityManager类的典型用法代码示例。如果您正苦于以下问题:C# AuthenticationIdentityManager类的具体用法?C# AuthenticationIdentityManager怎么用?C# AuthenticationIdentityManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AuthenticationIdentityManager类属于命名空间,在下文中一共展示了AuthenticationIdentityManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
string userName = UserName.Text;
var db = new ApplicationDbContext();
var manager = new AuthenticationIdentityManager(new IdentityStore(db));
ApplicationUser u = new ApplicationUser()
{
UserName = userName,
FirstName = tb_FirstName.Text,
LastName = tb_LastName.Text,
Email = tb_Email.Text
};
IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
if (result.Success)
{
manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
var foundRole = db.Roles.Single(x => x.Name == dl_Roles.SelectedValue);
u.Roles = new System.Collections.Generic.HashSet<UserRole>();
u.Roles.Add(new UserRole()
{
Role = foundRole,
User = u
});
db.SaveChanges();
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
示例2: LogIn
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
var context = new ApplicationDbContext();
var signinUser = context.Users.FirstOrDefault(u => u.UserName == UserName.Text);
if (signinUser!= null && !signinUser.IsDeleted)
{
// Validate the user password
IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
if (result.Success)
{
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
FailureText.Text = result.Errors.FirstOrDefault();
ErrorMessage.Visible = true;
}
}
else
{
FailureText.Text = "Please register";
ErrorMessage.Visible = true;
}
}
}
示例3: LogIn
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
if (result.Success)
{
var context = new ApplicationDbContext();
var curUserName = UserName.Text;
var userId = context.Users.FirstOrDefault(u => u.UserName == curUserName).Id;
var userRole = context.UserRoles.FirstOrDefault(u => u.UserId == userId).Role.Name;
if (userRole == "Banned")
{
ErrorSuccessNotifier.AddErrorMessage("You are banned!");
return;
}
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault());
}
}
}
示例4: LogIn
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
IdentityResult result = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
if (result.Success)
{
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
var errorMessageText = result.Errors.FirstOrDefault();
FailureText.Text = errorMessageText;
ErrorMessage.Visible = true;
ErrorSuccessNotifier.AddErrorMessage(errorMessageText);
}
}
else
{
ErrorSuccessNotifier.AddErrorMessage(
"An unexpected error occured! Please refresh the page...");
}
}
示例5: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
string firstName = FirstName.Text;
string lastName = LastName.Text;
string userName = UserName.Text;
string email = Email.Text;
var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
ApplicationUser u = new ApplicationUser()
{
FirstName = firstName,
LastName = lastName,
UserName = userName,
Email = email
};
IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
if (result.Success)
{
manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
示例6: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
//manager.Roles.CreateRoleAsync(new Role("Moderator"));
//manager.Roles.AddUserToRoleAsync("54fbcab5-d65b-47d4-b517-2eba10002e21", "186c2138-de27-42a6-94f0-8b830c2e8b9c");
var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
if (!manager.Logins.HasLocalLogin(User.Identity.GetUserId()))
{
grdMessages.EmptyDataTemplate = null;
if (grdMessages.FooterRow != null)
{
grdMessages.FooterRow.Visible = false;
}
}
else
{
var userId = User.Identity.GetUserId();
var db = new ApplicationDbContext();
var user = db.Users.FirstOrDefault(u => u.Id == userId);
if (user != null)
{
if (user.Roles.Any(r => r.Role.Name == "Moderator"))
{
grdMessages.Columns[2].Visible = true;
}
else if (user.Roles.Any(r => r.Role.Name == "Administrator"))
{
grdMessages.Columns[2].Visible = true;
grdMessages.Columns[3].Visible = true;
}
}
}
}
示例7: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
string userName = UserName.Text;
var manager = new AuthenticationIdentityManager(new IdentityStore());
User u = new User(userName) { UserName = userName };
IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
if (result.Success)
{
manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
//OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
using (SocialNetworkDbEntities context = new SocialNetworkDbEntities())
{
UserDetail details = new UserDetail();
context.UserDetails.Add(details);
context.SaveChanges();
var user = context.AspNetUsers.Find(u.Id);
user.UserDetailsId = details.UserDetailsId;
context.SaveChanges();
}
Response.Redirect("~/Account/UserDetails");
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
示例8: LinkButtonBanUser_Command
protected void LinkButtonBanUser_Command(object sender, CommandEventArgs e)
{
var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
string roleBanId = "2";
manager.Roles.AddUserToRoleAsync(e.CommandArgument.ToString(), roleBanId);
}
示例9: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
string userName = UserName.Text;
var manager = new AuthenticationIdentityManager(new IdentityStore(new AcademyDbContext()));
ApplicationUser u = new ApplicationUser(userName)
{
UserName = userName,
FirstName = this.TextBoxFirstName.Text,
LastName = this.TextBoxLastName.Text,
Email = this.TextBoxEmail.Text,
JoinDate = DateTime.Now,
};
var context = new AcademyDbContext();
string fileName = string.Empty;
var fileUpload = this.FileUploadAvatar;
if (fileUpload.HasFile)
{
if (fileUpload.PostedFile.ContentLength < 102400 &&
(fileUpload.PostedFile.ContentType == PngImageFormat ||
fileUpload.PostedFile.ContentType == JpegImageFormat ||
fileUpload.PostedFile.ContentType == GifImageFormat))
{
fileName = userName.Replace("<", string.Empty).Replace(">", string.Empty) + GetAvatarExtension(FileUploadAvatar.PostedFile.FileName);
fileUpload.SaveAs(Server.MapPath(MainPath) + fileName);
u.AvatarPath = MainPath + fileName;
}
else
{
ErrorSuccessNotifier.ShowAfterRedirect = true;
ErrorSuccessNotifier.AddErrorMessage("The uploaded avatar exceeds 100KB or is in a wrong format.");
Response.Redirect(Request.RawUrl, false);
return;
}
}
else
{
u.AvatarPath = DefaultImagePath;
}
IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
if (result.Success)
{
manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
ErrorSuccessNotifier.ShowAfterRedirect = true;
ErrorSuccessNotifier.AddSuccessMessage("Registration completed successfully.");
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault());
}
}
示例10: AccountController
public AccountController()
{
var context = new MoviesContext();
userRole = context.Roles
.Where(r => r.Name == "user")
.Select(r => r.Id)
.FirstOrDefault()
.ToString();
IdentityManager = new AuthenticationIdentityManager(new IdentityStore(context));
}
示例11: Page_Load
protected void Page_Load()
{
// Process the result from an auth provider in the request
ProviderName = OpenAuthProviders.GetProviderNameFromRequest(Request);
if (String.IsNullOrEmpty(ProviderName))
{
Response.Redirect("~/Account/Login");
}
if (!IsPostBack)
{
IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
var auth = Context.GetOwinContext().Authentication;
ClaimsIdentity id = manager.GetExternalIdentity(auth);
var context = new ApplicationDbContext();
var user = context.UserRoles.FirstOrDefault(u => u.User.UserName == id.Name);
if (user != null)
{
var userRole = user.Role.Name;
if (userRole == "Banned")
{
ErrorSuccessNotifier.AddErrorMessage("You are banned!");
ErrorSuccessNotifier.ShowAfterRedirect = true;
Response.Redirect("~/Account/Login");
}
}
IdentityResult result = manager.SignInExternalIdentity(auth, id);
if (result.Success)
{
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else if (User.Identity.IsAuthenticated)
{
result = manager.LinkExternalIdentity(id, User.Identity.GetUserId());
if (result.Success)
{
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
AddErrors(result);
return;
}
}
else
{
userName.Text = id.Name;
}
}
}
示例12: Redirect
private async void Redirect()
{
AuthenticationIdentityManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
var userId = User.Identity.GetUserId();
var roles = await manager.Roles.GetRolesForUserAsync(userId);
if (roles.Any(r => r.Name == "Administrator"))
{
Response.Redirect("~/Administrator/AdministratorDefault.aspx");
}
else if (roles.Any(r => r.Name == "Moderator"))
{
Response.Redirect("~/Moderator/ModeratorDefault.aspx");
}
}
示例13: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
string userName = UserName.Text;
string email = Email.Text;
string filename = "default.png";
if (UploadAvatar.HasFile)
{
if (UploadAvatar.PostedFile.ContentType == "image/jpeg" ||
UploadAvatar.PostedFile.ContentType == "image/gif" ||
UploadAvatar.PostedFile.ContentType == "image/png")
{
if (UploadAvatar.PostedFile.ContentLength < 102400)
{
filename = userName + Path.GetExtension(UploadAvatar.FileName);
UploadAvatar.SaveAs(Server.MapPath("~/Avatar_Files/") + filename);
}
else
{
ErrorSuccessNotifier.AddErrorMessage("Upload status: The file has to be less than 100 kb!");
return;
}
}
else
{
ErrorSuccessNotifier.AddErrorMessage("Upload status: Only JPEG files are accepted!");
return;
}
}
var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
ApplicationUser u = new ApplicationUser()
{
UserName = userName,
Email = email,
Avatar = filename
};
IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
if (result.Success)
{
manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault());
}
}
示例14: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
string userName = UserName.Text;
var manager = new AuthenticationIdentityManager(new IdentityStore());
User u = new User(userName) { UserName = userName };
IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);
if (result.Success)
{
manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
OpenAuthProviders.RedirectToReturnUrl("~/library-system/Default.aspx", Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
示例15: CreateUser_Click
protected void CreateUser_Click(object sender, EventArgs e)
{
string userName = this.UserName.Text;
var manager = new AuthenticationIdentityManager(new IdentityStore(new BooksLibrarySystemContext()));
User u = new User(userName) { UserName = userName };
IdentityResult result = manager.Users.CreateLocalUser(u, this.Password.Text);
if (result.Success)
{
manager.Authentication.SignIn(this.Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
this.Response.Redirect(this.Request.QueryString["ReturnUrl"], false);
//BooksLibrarySystem.Web.Account.OpenAuthProviders.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
}
else
{
this.ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}