-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLogonAuthController.cs
105 lines (101 loc) · 5.15 KB
/
LogonAuthController.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Web.Utils;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OpenIdConnect;
namespace MySolution.Module.Web.Controllers {
public class LogonAuthController : ViewController<DetailView> {
public const string OAuthParameter = "oauth";
private SimpleAction microsoftAction;
public LogonAuthController() {
TargetObjectType = typeof(AuthenticationStandardLogonParameters);
microsoftAction = new SimpleAction(this, "LoginWithMicrosoft", "OAuthActions");
microsoftAction.Execute += microsoftAccountAction_Execute;
microsoftAction.Caption = "Microsoft";
}
protected override void OnActivated() {
base.OnActivated();
LogonController logonController = Frame.GetController<LogonController>();
if (logonController != null) {
logonController.AcceptAction.Changed += AcceptAction_Changed;
}
}
private void Challenge(string provider) {
var uriBuilder = new UriBuilder(HttpContext.Current.Request.Url);
var queryString = HttpUtility.ParseQueryString(uriBuilder.Query);
queryString["oauth"] = "true";
string redirectUrl = WebApplication.LogonPage + "?" + queryString.ToString();
AuthenticationProperties properties = new AuthenticationProperties();
properties.RedirectUri = redirectUrl;
properties.Dictionary["Provider"] = provider;
HttpContext.Current.GetOwinContext().Authentication.Challenge(properties, provider);
}
private void microsoftAccountAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
private IList<string> GetProviderNames() {
IList<AuthenticationDescription> descriptions = HttpContext.Current.GetOwinContext().Authentication.GetAuthenticationTypes((AuthenticationDescription d) => d.Properties != null && d.Properties.ContainsKey("Caption")) as IList<AuthenticationDescription>;
List<string> providersNames = new List<string>();
foreach (AuthenticationDescription description in descriptions) {
providersNames.Add(description.AuthenticationType);
}
return providersNames;
}
private void CurrentRequestPage_Load(object sender, System.EventArgs e) {
((Page)sender).Load -= CurrentRequestPage_Load;
LogonController logonController = Frame.GetController<LogonController>();
if (logonController != null && logonController.AcceptAction.Active) {
((ISupportMixedAuthentication)Application.Security).AuthenticationMixed.SetupAuthenticationProvider("OAuthProvider");
logonController.AcceptAction.DoExecute();
}
}
private void AcceptAction_Changed(object sender, ActionChangedEventArgs e) {
if (e.ChangedPropertyType == ActionChangedType.Active) {
SetActionsActive(((ActionBase)sender).Active);
}
}
private void SetActionsActive(bool logonActionActive) {
foreach (ActionBase action in Actions) {
action.Active["LogonActionActive"] = logonActionActive;
}
RegisterVisibleUserExistingTextScript(logonActionActive);
}
private void RegisterVisibleUserExistingTextScript(bool visible) {
((WebWindow)Frame).RegisterClientScript("LogonActionActive",
string.Format("SetVisibleUserExistingText({0});", ClientSideEventsHelper.ToJSBoolean(visible)), true);
}
protected override void OnViewControlsCreated() {
LogonController logonController = Frame.GetController<LogonController>();
if (logonController != null) {
SetActionsActive(logonController.AcceptAction.Active);
}
IList<string> providersName = GetProviderNames() as IList<string>;
if (providersName.Count == 0) {
RegisterVisibleUserExistingTextScript(false);
}
microsoftAction.Active["ProviderIsSet"] = providersName.Contains(OpenIdConnectAuthenticationDefaults.AuthenticationType);
if (IsOAuthRequest && WebWindow.CurrentRequestPage != null) {
WebWindow.CurrentRequestPage.Load += CurrentRequestPage_Load;
}
base.OnViewControlsCreated();
}
public static bool IsOAuthRequest {
get {
return HttpContext.Current.Request.Params[OAuthParameter] != null;
}
}
protected override void OnDeactivated() {
LogonController logonController = Frame.GetController<LogonController>();
if (logonController != null) {
logonController.AcceptAction.Changed -= AcceptAction_Changed;
}
base.OnDeactivated();
}
}
}