gpt4 book ai didi

java - ASP MVC5 UserManager 添加方法

转载 作者:行者123 更新时间:2023-11-30 07:07:16 29 4
gpt4 key购买 nike

我想编辑 UserManager 类以添加一些方法,例如 ChangeEmailAsync(ChangePasswordAsync 的克隆)我是 C# 和 . NET 因为我曾经在 java 中工作,所以我正在阅读 Microsoft 网站上的入门书并练习一些表格。

我看到一行代码:

IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

我想查看 UserManager 中的代码来复制 ChangePassword 并创建 ChangeEmail,并了解创建一些方法(更改其他字段)的过程等)

我使用 F12 导航到 UserManager,我看到它是一个接口(interface)(或者看起来像 java 接口(interface))

 #region Ensamblado Microsoft.AspNet.Identity.Core.dll, v1.0.0.0
// C:\Users\User\Documents\Visual Studio 2013\Projects\MTGWeb\packages\Microsoft.AspNet.Identity.Core.1.0.0\lib\net45\Microsoft.AspNet.Identity.Core.dll
#endregion

using System;
using System.Diagnostics;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Microsoft.AspNet.Identity
{
public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
{
public UserManager(IUserStore<TUser> store);

public ClaimsIdentityFactory<TUser> ClaimsIdentityFactory { get; set; }
public IPasswordHasher PasswordHasher { get; set; }
public IIdentityValidator<string> PasswordValidator { get; set; }
protected IUserStore<TUser> Store { get; }
public virtual bool SupportsUserClaim { get; }
public virtual bool SupportsUserLogin { get; }
public virtual bool SupportsUserPassword { get; }
public virtual bool SupportsUserRole { get; }
public virtual bool SupportsUserSecurityStamp { get; }
public IIdentityValidator<TUser> UserValidator { get; set; }

[DebuggerStepThrough]
public virtual Task<IdentityResult> AddClaimAsync(string userId, Claim claim);
[DebuggerStepThrough]
public virtual Task<IdentityResult> AddLoginAsync(string userId, UserLoginInfo login);
[DebuggerStepThrough]
public virtual Task<IdentityResult> AddPasswordAsync(string userId, string password);
[DebuggerStepThrough]
public virtual Task<IdentityResult> AddToRoleAsync(string userId, string role);
[DebuggerStepThrough]
public virtual Task<IdentityResult> ChangePasswordAsync(string userId, string currentPassword, string newPassword);
[DebuggerStepThrough]
public virtual Task<IdentityResult> CreateAsync(TUser user);
[DebuggerStepThrough]
public virtual Task<IdentityResult> CreateAsync(TUser user, string password);
public virtual Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType);
public void Dispose();
protected virtual void Dispose(bool disposing);
public virtual Task<TUser> FindAsync(UserLoginInfo login);
[DebuggerStepThrough]
public virtual Task<TUser> FindAsync(string userName, string password);
[DebuggerStepThrough]
public virtual Task<TUser> FindByIdAsync(string userId);
[DebuggerStepThrough]
public virtual Task<TUser> FindByNameAsync(string userName);
[DebuggerStepThrough]
public virtual Task<System.Collections.Generic.IList<Claim>> GetClaimsAsync(string userId);
[DebuggerStepThrough]
public virtual Task<System.Collections.Generic.IList<UserLoginInfo>> GetLoginsAsync(string userId);
[DebuggerStepThrough]
public virtual Task<System.Collections.Generic.IList<string>> GetRolesAsync(string userId);
[DebuggerStepThrough]
public virtual Task<bool> HasPasswordAsync(string userId);
[DebuggerStepThrough]
public virtual Task<bool> IsInRoleAsync(string userId, string role);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemoveClaimAsync(string userId, Claim claim);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemoveFromRoleAsync(string userId, string role);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemoveLoginAsync(string userId, UserLoginInfo login);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemovePasswordAsync(string userId);
[DebuggerStepThrough]
public virtual Task<IdentityResult> UpdateAsync(TUser user);
[DebuggerStepThrough]
public virtual Task<IdentityResult> UpdateSecurityStampAsync(string userId);
}
}


I can implement this? And I can see the code of the methods.

#region Ensamblado Microsoft.AspNet.Identity.Core.dll, v1.0.0.0
// C:\Users\User\Documents\Visual Studio 2013\Projects\MTGWeb\packages\Microsoft.AspNet.Identity.Core.1.0.0\lib\net45\Microsoft.AspNet.Identity.Core.dll
#endregion

using System;
using System.Diagnostics;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Microsoft.AspNet.Identity
{
public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
{
public UserManager(IUserStore<TUser> store);

public ClaimsIdentityFactory<TUser> ClaimsIdentityFactory { get; set; }
public IPasswordHasher PasswordHasher { get; set; }
public IIdentityValidator<string> PasswordValidator { get; set; }
protected IUserStore<TUser> Store { get; }
public virtual bool SupportsUserClaim { get; }
public virtual bool SupportsUserLogin { get; }
public virtual bool SupportsUserPassword { get; }
public virtual bool SupportsUserRole { get; }
public virtual bool SupportsUserSecurityStamp { get; }
public IIdentityValidator<TUser> UserValidator { get; set; }

[DebuggerStepThrough]
public virtual Task<IdentityResult> AddClaimAsync(string userId, Claim claim);
[DebuggerStepThrough]
public virtual Task<IdentityResult> AddLoginAsync(string userId, UserLoginInfo login);
[DebuggerStepThrough]
public virtual Task<IdentityResult> AddPasswordAsync(string userId, string password);
[DebuggerStepThrough]
public virtual Task<IdentityResult> AddToRoleAsync(string userId, string role);
[DebuggerStepThrough]
public virtual Task<IdentityResult> ChangePasswordAsync(string userId, string currentPassword, string newPassword);
[DebuggerStepThrough]
public virtual Task<IdentityResult> CreateAsync(TUser user);
[DebuggerStepThrough]
public virtual Task<IdentityResult> CreateAsync(TUser user, string password);
public virtual Task<ClaimsIdentity> CreateIdentityAsync(TUser user, string authenticationType);
public void Dispose();
protected virtual void Dispose(bool disposing);
public virtual Task<TUser> FindAsync(UserLoginInfo login);
[DebuggerStepThrough]
public virtual Task<TUser> FindAsync(string userName, string password);
[DebuggerStepThrough]
public virtual Task<TUser> FindByIdAsync(string userId);
[DebuggerStepThrough]
public virtual Task<TUser> FindByNameAsync(string userName);
[DebuggerStepThrough]
public virtual Task<System.Collections.Generic.IList<Claim>> GetClaimsAsync(string userId);
[DebuggerStepThrough]
public virtual Task<System.Collections.Generic.IList<UserLoginInfo>> GetLoginsAsync(string userId);
[DebuggerStepThrough]
public virtual Task<System.Collections.Generic.IList<string>> GetRolesAsync(string userId);
[DebuggerStepThrough]
public virtual Task<bool> HasPasswordAsync(string userId);
[DebuggerStepThrough]
public virtual Task<bool> IsInRoleAsync(string userId, string role);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemoveClaimAsync(string userId, Claim claim);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemoveFromRoleAsync(string userId, string role);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemoveLoginAsync(string userId, UserLoginInfo login);
[DebuggerStepThrough]
public virtual Task<IdentityResult> RemovePasswordAsync(string userId);
[DebuggerStepThrough]
public virtual Task<IdentityResult> UpdateAsync(TUser user);
[DebuggerStepThrough]
public virtual Task<IdentityResult> UpdateSecurityStampAsync(string userId);
}
}

非常感谢您的帮助,对不起我的英语

最佳答案

您不能直接向此类添加新方法,因为它来自另一个程序集。我明白您为什么将其称为接口(interface),但是从另一个程序集导航到一个类实际上只会向您显示该类的构造函数、属性和公共(public)方法的不同签名。你不能改变这些。

要更改用户的属性,您应该在 UserManager 上使用 UpdateAsync(TUser user) 方法,也许像这样(在您的 Controller 中):

    [HttpPost]
public async Task<ActionResult> ChangeEmail(string newEmail)
{
var user = await UserManager.FindByNameAsync(User.Identity.Name);
user.Email = newEmail;
UserManager.UpdateAsync(user);
return View();
}

关于java - ASP MVC5 UserManager 添加方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25021756/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com