Using ASP.Net Identity in MVC 4

有些话、适合烂在心里 提交于 2019-12-20 09:04:15

问题


I've been reading about the new auth stuff in the upcoming versions of ASP.net: http://blogs.msdn.com/b/webdev/archive/2013/06/27/introducing-asp-net-identity-membership-system-for-asp-net-applications.aspx

I'm creating a new ASP.net MVC 4 project in visual studio 2012 and I'd like to use the new auth bits if I can. Is this possible?

I'm reading code and trying to wrap my head around this new API. But in the meantime, what steps are involved to get going?


回答1:


It should be doable, first you basically want to install the 3 packages:

Microsoft.AspNet.Identity.Core
Microsoft.AspNet.Identity.EntityFramework
Microsoft.AspNet.Identity.Owin

You would then need to pull in the associated Owin packages as well:

Owin
Microsoft.Owin
Microsoft.Owin.Security
Microsoft.Owin.Security.Cookies
Microsoft.Owin.Host.SystemWeb

And you would then need to hookup Owin with something like this:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(WebApplication19.Startup))]
namespace WebApplication19
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
                // Enable the application to use a cookie to store information for the signed in user
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/Account/Login")
                });
                // Use a cookie to temporarily store information about a user logging in with a third party login provider
                app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        }
    }
}

You will also have to remove/turn off all of the old membership/forms auth in your app, and switch to using the new identity APIs.



来源:https://stackoverflow.com/questions/19237285/using-asp-net-identity-in-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!