Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.EntityFramework;
- using Microsoft.Owin;
- using Microsoft.Owin.Security;
- using Microsoft.Owin.Security.Cookies;
- using Microsoft.Owin.Security.Facebook;
- using Owin;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using System.Web;
- namespace MyProject
- {
- public partial class Startup
- {
- // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
- public void ConfigureAuth(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")
- });
- // Use a cookie to temporarily store information about a user logging in with a third party login provider
- app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
- var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
- {
- Provider = new FacebookAuthenticationProvider()
- {
- OnAuthenticated = (context) =>
- {
- var rawUserObjectFromFacebookAsJson = context.User;
- var userManager = new UserManager<IdentityUser>(new CustomUserStore());
- var username = rawUserObjectFromFacebookAsJson.Value<string>("username");
- var user = new IdentityUser(username);
- //var identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
- var identity = new ClaimsIdentity("Application");
- identity.AddClaim(new Claim(ClaimTypes.Name, "<user name>"));
- HttpContext.Current.GetOwinContext().Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant(identity, new AuthenticationProperties()
- {
- IsPersistent = true
- });
- //HttpContext.Current.GetOwinContext().Authentication.SignIn(new AuthenticationProperties
- //{
- // IsPersistent = true
- //}, identity);
- return Task.FromResult(0);
- }
- },
- AppId = "xxxxxxxxxxxx",
- AppSecret = "yyyyyyyyyyyyyyyyyyyyyyyy"
- };
- facebookOptions.Scope.Add("email");
- app.UseFacebookAuthentication(facebookOptions);
- }
- }
- public class CustomUserStore : UserStore<IdentityUser>
- {
- public override Task<IdentityUser> FindAsync(UserLoginInfo login)
- {
- return base.FindAsync(login);
- }
- public override Task<IdentityUser> FindByNameAsync(string userName)
- {
- return this.FindByIdAsync(userName);
- }
- public override Task<IdentityUser> FindByIdAsync(string userId)
- {
- return Task.Run<IdentityUser>(() =>
- {
- return new IdentityUser
- {
- Id = userId,
- UserName = userId
- };
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement