Repository navigation
Expand file tree
/
Copy pathFacebookAuthenticationModule.cs
More file actions
47 lines (42 loc) · 1.67 KB
/
Copy pathFacebookAuthenticationModule.cs
File metadata and controls
47 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Security.Claims;
using System.Threading;
using System.Web;
using Facebook;
using Microsoft.AspNet.Facebook.Client;
namespace Microsoft.AspNet.Facebook
{
internal sealed class FacebookAuthenticationModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.AuthenticateRequest += (sender, e) =>
{
HttpContext httpContext = ((HttpApplication)sender).Context;
dynamic signedRequest = FacebookRequestHelpers.GetSignedRequest(
new HttpContextWrapper(httpContext),
rawSignedRequest =>
{
FacebookConfiguration config = GlobalFacebookConfiguration.Configuration;
FacebookClient client = config.ClientProvider.CreateClient();
return client.ParseSignedRequest(rawSignedRequest);
});
if (signedRequest != null)
{
string userId = signedRequest.user_id;
if (!String.IsNullOrEmpty(userId))
{
ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, userId) }));
Thread.CurrentPrincipal = principal;
httpContext.User = principal;
}
}
};
}
public void Dispose()
{
}
}
}