【问题标题】:Token Based Authentication using ASP.NET Web API 2, Owin, AspNet.Identity.MongoDB and mongocsharpdriver使用 ASP.NET Web API 2、Owin、AspNet.Identity.MongoDB 和 mongocsharpdriver 的基于令牌的身份验证
【发布时间】:2014-11-04 10:53:01
【问题描述】:

+在成功实现使用 ASP.NET Web API 2、Owin 和 Identity 的基于令牌的身份验证后,我希望在 Entity Framework 的帮助下将我的实现更改为使用 MongoDB 而不是 MSSQL这个application here....说实话,我不完全理解应该怎么做,但至少我知道我希望我的应用程序表现什么。我想关注这个IMPLEMENTATION HERE,使用 AspNet.Identity.MongoDB mongocsharpdriver...到目前为止,这就是我已经完成: 客户经理

using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using withMongoDB.HelperClasses.Services;
using withMongoDB.Models.Account;

namespace withMongoDB.Controllers
{
    [RoutePrefix("api/Account")]
    public class AccountsController : ApiController
    {
        AccountsService _accountsService = new AccountsService();

        // POST api/Account/Register
        [AllowAnonymous]
        [Route("Register")]
        public async Task<IHttpActionResult> Register(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            await _accountsService.Register(userModel);

            return Ok();
        }

        private IHttpActionResult GetErrorResult(IdentityResult result)
        {
            if (result == null)
            {
                return InternalServerError();
            }

            if (!result.Succeeded)
            {
                if (result.Errors != null)
                {
                    foreach (string error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }

                if (ModelState.IsValid)
                {
                    // No ModelState errors are available to send, so just return an empty BadRequest.
                    return BadRequest();
                }

                return BadRequest(ModelState);
            }

            return null;
        }
    }
}

那么控制器的注册方法应该由accounts Service

using AspNet.Identity.MongoDB;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using withMongoDB.Models.Account;

namespace withMongoDB.HelperClasses.Services
{
    public class AccountsService 
    {
        private readonly MongoAccountsConnectionHelper<UserProfile> _accounts;

        public AccountsService()
        {
            _accounts = new MongoAccountsConnectionHelper<UserProfile>();
        }

        public async Task<IdentityResult> Register(UserModel userModel)
        {
            var userprofile = new UserProfile
            {
                UserName = userModel.UserName
            };

            var result = await _accounts.CreateAsync(userprofile, userModel.Password);

            return result;
        }
    }
}

最后 MongoAccountsConnectionHelper 将帐户服务类的结果带到 mongo 数据库....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace withMongoDB.HelperClasses 
{
    using AspNet.Identity.MongoDB;
    //using Entities;
    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Conventions;
    using MongoDB.Driver;
    using System.Configuration;
    using withMongoDB.Models.Account;
    public class MongoAccountsConnectionHelper
    {
        private readonly MongoCollection<UserProfile> userProfileCollection;
        public MongoDatabase Database { get; private set; }
        public MongoAccountsConnectionHelper()
        {
            var pack = new ConventionPack()
            {
                new CamelCaseElementNameConvention(),
                new EnumRepresentationConvention(BsonType.String)
            };

            ConventionRegistry.Register("CamelCaseConvensions", pack, t => true);

            var mongoUrlBuilder = new MongoConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);
            Database = new MongoClient().GetServer().GetDatabase(mongoUrlBuilder.DatabaseName);

            userProfileCollection = Database.GetCollection<UserProfile>("users");
        }

        public MongoCollection<UserProfile> Users
        {
            get { return userProfileCollection; }
        }
    }
}

任何帮助、提示、想法或意见将不胜感激......{我是否应该考虑替代方案,例如 brockallen 的 MembershipReboot 和 IdentityReboot?}

【问题讨论】:

    标签: mongodb asp.net-web-api claims-based-identity


    【解决方案1】:

    要顺利完成,您首先需要通过提供您自己的用户类/表(至少应实现 IUser)来消除对“Microsoft.AspNet.Identity.EntityFramework”的依赖,并且您还需要实现UserManager 类(它应该实现 UserManager,最后你需要实现 UserStore 类(它应该至少实现 IUserStore),其中 T 是 User Table 中的 Id 类型。

    完成上述操作后,您可以更改为使用 MongoDB 的 UserStore。

    希望对您有所帮助。

    【讨论】:

    • +感谢奥马尔的回复。我设法做到了,但与示例项目 {github.com/attilah/AngularJSAuthentication} 中的完全一样,包括使用 autofac...我发布了另一个关于如何删除 autofac {stackoverflow.com/questions/26829498/…} 的问题,我希望你能提供帮助。 ..一旦我学会了 autofac,我将使用它
    • @McKabue - 我也指的是同一个示例项目,但我找不到 IdentityContext 文件。请您分享位置
    猜你喜欢
    • 2017-03-19
    • 2014-11-27
    • 2016-06-09
    • 2013-11-25
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多