ASP MVC5 UserManager添加方法

时间:2014-07-29 17:46:32

标签: java c# asp.net asp.net-mvc

我希望编辑UserManager类添加一些方法,例如ChangeEmailAsync(克隆ChangePasswordAsync)我是C# and .NET的新手,因为我以前在java工作,我是阅读微软网站的启动并练习一些表格。

我看到一行代码:

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

我希望看到UserManager中的代码复制ChangePassword并创建ChangeEmail,并了解创建某些方法的过程(更改其他字段等)

我使用 F12 导航到UserManager,我看到它的界面(或看起来像java界面)

 #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);
        }
    }

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

1 个答案:

答案 0 :(得分:4)

您无法直接向此类添加新方法,因为它来自另一个程序集。我可以看到你为什么称它为接口,但从另一个程序集导航到一个类实际上只显示了类的构造函数,属性和公共方法的不同签名。你无法改变这些。

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

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