Advertisement
Guest User

Owin Auth

a guest
Nov 3rd, 2013
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using Microsoft.AspNet.Identity;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using Microsoft.Owin;
  4. using Microsoft.Owin.Security;
  5. using Microsoft.Owin.Security.Cookies;
  6. using Microsoft.Owin.Security.Facebook;
  7. using Owin;
  8. using System.Security.Claims;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11.  
  12. namespace MyProject
  13. {
  14.     public partial class Startup
  15.     {
  16.         // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
  17.         public void ConfigureAuth(IAppBuilder app)
  18.         {
  19.             // Enable the application to use a cookie to store information for the signed in user
  20.             app.UseCookieAuthentication(new CookieAuthenticationOptions
  21.             {
  22.                 AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  23.                 LoginPath = new PathString("/Account")
  24.             });
  25.  
  26.             // Use a cookie to temporarily store information about a user logging in with a third party login provider
  27.             app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
  28.  
  29.  
  30.             var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
  31.             {
  32.                 Provider = new FacebookAuthenticationProvider()
  33.                 {
  34.                     OnAuthenticated = (context) =>
  35.                         {
  36.                             var rawUserObjectFromFacebookAsJson = context.User;
  37.  
  38.                             var userManager = new UserManager<IdentityUser>(new CustomUserStore());
  39.  
  40.                             var username = rawUserObjectFromFacebookAsJson.Value<string>("username");
  41.                             var user = new IdentityUser(username);
  42.                             //var identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
  43.  
  44.                             var identity = new ClaimsIdentity("Application");
  45.                             identity.AddClaim(new Claim(ClaimTypes.Name, "<user name>"));
  46.                             HttpContext.Current.GetOwinContext().Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant(identity, new AuthenticationProperties()
  47.                             {
  48.                                 IsPersistent = true
  49.                             });
  50.  
  51.                             //HttpContext.Current.GetOwinContext().Authentication.SignIn(new AuthenticationProperties
  52.                             //{
  53.                             //    IsPersistent = true
  54.                             //}, identity);
  55.  
  56.                             return Task.FromResult(0);
  57.                         }
  58.                 },
  59.                 AppId = "xxxxxxxxxxxx",
  60.                 AppSecret = "yyyyyyyyyyyyyyyyyyyyyyyy"
  61.             };
  62.  
  63.             facebookOptions.Scope.Add("email");
  64.  
  65.             app.UseFacebookAuthentication(facebookOptions);
  66.  
  67.         }
  68.     }
  69.  
  70.     public class CustomUserStore : UserStore<IdentityUser>
  71.     {
  72.         public override Task<IdentityUser> FindAsync(UserLoginInfo login)
  73.         {
  74.             return base.FindAsync(login);
  75.         }
  76.  
  77.         public override Task<IdentityUser> FindByNameAsync(string userName)
  78.         {
  79.             return this.FindByIdAsync(userName);
  80.         }
  81.  
  82.         public override Task<IdentityUser> FindByIdAsync(string userId)
  83.         {
  84.             return Task.Run<IdentityUser>(() =>
  85.             {
  86.                 return new IdentityUser
  87.                 {
  88.                     Id = userId,
  89.                     UserName = userId
  90.                 };
  91.             });
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement