forked from aspnet/AzureSignalR-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
87 lines (75 loc) · 3.1 KB
/
Startup.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.SignalR.Samples.ChatRoom
{
public class Startup
{
private const string GitHubClientId = "GitHubClientId";
private const string GitHubClientSecret = "GitHubClientSecret";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie()
.AddGitHub(options =>
{
options.ClientId = Configuration[GitHubClientId];
options.ClientSecret = Configuration[GitHubClientSecret];
options.Scope.Add("user:email");
options.Events = new OAuthEvents
{
OnCreatingTicket = GetUserCompanyInfoAsync
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("Microsoft_Only", policy => policy.RequireClaim("Company", "Microsoft"));
});
services.AddMvc();
services.AddSignalR()
.AddAzureSignalR();
}
public void Configure(IApplicationBuilder app)
{
app.UseAuthentication();
app.UseMvc();
app.UseFileServer();
app.UseAzureSignalR(routes =>
{
routes.MapHub<Chat>("/chat");
});
}
private static async Task GetUserCompanyInfoAsync(OAuthCreatingTicketContext context)
{
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
var response = await context.Backchannel.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
if (user.ContainsKey("company"))
{
var company = user["company"].ToString();
var companyIdentity = new ClaimsIdentity(new[]
{
new Claim("Company", company)
});
context.Principal.AddIdentity(companyIdentity);
}
}
}
}