接着在MVC项目内加入下列程序代码,用以挂载与设定FacebookAuthenticationMiddleware。在这其中AppId、AppSecret是Facebook开发者中心提供的APP账号数据,而Scope、UserInformationEndpoint两个参数则是定义要额外取得用户的E-Mail信息。
public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // Authentication app.UseFacebookAuthentication(options => { options.AppId = "770764239696406"; options.AppSecret = "2eecc0b9ef785e43bcd4779e2803ba0f"; options.Scope.Add("email"); options.UserInformationEndpoint = "https://graph.facebook.com/v2.5/me?fields=id,name,email"; }); } }再来打开AccountController加入下列程序代码以及对应的View,用以提供ASP.NET站台处理Facebook这类的第三方登入(ExternalLogin)。在这其中,ExternalLogin用来发起一个验证挑战(Challenge),系统会依照externalProvider参数,来决定是要向Facebook或是其他第三方系统做验证。
当用户通过验证后,系统会调用ExternalLoginCallback来处理验证结果。在ExternalLoginCallback里会取得验证结果中FBUser的UserId,用来与ExistingIdentitySystem做验证。如果验证通过,会接着从ExistingIdentitySystem取得对应的ExistingUser、再转换为APPUser来真正登入系统。(关于程序代码的相关背景知识,请参阅技术剖析说明:ASP.NET Identity登入技术剖析)
public class AccountController : Controller { public IActionResult ExternalLogin(string externalProvider, string returnUrl = null) { // AuthenticationProperties var authenticationProperties = new AuthenticationProperties(); authenticationProperties.Items.Add("ExternalProvider", externalProvider); authenticationProperties.RedirectUri = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }); // Return return new ChallengeResult(externalProvider, authenticationProperties); } public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null) { // AuthenticateContext var authenticateContext = new AuthenticateContext(IdentityOptions.Current.ExternalCookieAuthenticationScheme); await this.HttpContext.Authentication.AuthenticateAsync(authenticateContext); // AuthenticateInfo string userId = authenticateContext.Principal.FindFirst(ClaimTypes.Email).Value; string externalProvider = authenticateContext.Properties["ExternalProvider"] as string; // Login var existingIdentitySystem = new ExistingIdentitySystem(); if (existingIdentitySystem.ExternalSignIn(userId, externalProvider) == false) { throw new InvalidOperationException(); } // ExistingUser var existingUser = existingIdentitySystem.GetUserById(userId); if (existingUser == null) throw new InvalidOperationException(); // ApplicationUser var applicationIdentity = new ClaimsIdentity(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role); applicationIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, existingUser.Id)); applicationIdentity.AddClaim(new Claim(ClaimTypes.Name, existingUser.Name)); var applicationUser = new ClaimsPrincipal(applicationIdentity); // Cookie await this.HttpContext.Authentication.SignInAsync(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, applicationUser); await this.HttpContext.Authentication.SignOutAsync(IdentityOptions.Current.ExternalCookieAuthenticationScheme); // Return return Redirect(returnUrl); } } 开发 - Password Authentication完成上述步骤后,接着着手开发Password验证。打开AccountController加入下列程序代码以及对应的View,用以提供ASP.NET站台处理Password验证。在这其中,PasswordLogin会接收用户输入的账号密码,用来与ExistingIdentitySystem做验证。如果验证通过,会接着从ExistingIdentitySystem取得ExistingUser、再转换为APPUser来真正登入系统。(关于程序代码的相关背景知识,请参阅技术剖析说明:ASP.NET Identity登入技术剖析)
public class AccountController : Controller { public async Task<IActionResult> PasswordLogin(string userId, string password, string returnUrl = null) { // Login var existingIdentitySystem = new ExistingIdentitySystem(); if (existingIdentitySystem.PasswordSignIn(userId, password) == false) { throw new InvalidOperationException(); } // ExistingUser var existingUser = existingIdentitySystem.GetUserById(userId); if (existingUser == null) throw new InvalidOperationException(); // ApplicationUser var applicationIdentity = new ClaimsIdentity(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role); applicationIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, existingUser.Id)); applicationIdentity.AddClaim(new Claim(ClaimTypes.Name, existingUser.Name)); var applicationUser = new ClaimsPrincipal(applicationIdentity); // Cookie await this.HttpContext.Authentication.SignInAsync(IdentityOptions.Current.ApplicationCookieAuthenticationScheme, applicationUser); await this.HttpContext.Authentication.SignOutAsync(IdentityOptions.Current.ExternalCookieAuthenticationScheme); // Return return Redirect(returnUrl); } } 使用