Update 07082021
This commit is contained in:
98
WebFormApp/Account/ManagePassword.aspx.cs
Normal file
98
WebFormApp/Account/ManagePassword.aspx.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Identity.Owin;
|
||||
|
||||
namespace WebFormApp.Account
|
||||
{
|
||||
public partial class ManagePassword : System.Web.UI.Page
|
||||
{
|
||||
protected string SuccessMessage
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
private bool HasPassword(ApplicationUserManager manager)
|
||||
{
|
||||
return manager.HasPassword(User.Identity.GetUserId());
|
||||
}
|
||||
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
{
|
||||
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
|
||||
|
||||
if (!IsPostBack)
|
||||
{
|
||||
// Zu rendernde Abschnitte ermitteln
|
||||
if (HasPassword(manager))
|
||||
{
|
||||
changePasswordHolder.Visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
setPassword.Visible = true;
|
||||
changePasswordHolder.Visible = false;
|
||||
}
|
||||
|
||||
// Erfolgsmeldung rendern
|
||||
var message = Request.QueryString["m"];
|
||||
if (message != null)
|
||||
{
|
||||
// Abfragezeichenfolge aus der Aktion entfernen
|
||||
Form.Action = ResolveUrl("~/Account/Manage");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void ChangePassword_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (IsValid)
|
||||
{
|
||||
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
|
||||
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
|
||||
IdentityResult result = manager.ChangePassword(User.Identity.GetUserId(), CurrentPassword.Text, NewPassword.Text);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
var user = manager.FindById(User.Identity.GetUserId());
|
||||
signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
|
||||
Response.Redirect("~/Account/Manage?m=ChangePwdSuccess");
|
||||
}
|
||||
else
|
||||
{
|
||||
AddErrors(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void SetPassword_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (IsValid)
|
||||
{
|
||||
// Lokale Anmeldeinformationen erstellen und das lokale Konto mit dem Benutzer verknüpfen
|
||||
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
|
||||
IdentityResult result = manager.AddPassword(User.Identity.GetUserId(), password.Text);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
Response.Redirect("~/Account/Manage?m=SetPwdSuccess");
|
||||
}
|
||||
else
|
||||
{
|
||||
AddErrors(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddErrors(IdentityResult result)
|
||||
{
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
ModelState.AddModelError("", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user