-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathAccountController.Windows.cs
More file actions
123 lines (108 loc) · 3.43 KB
/
AccountController.Windows.cs
File metadata and controls
123 lines (108 loc) · 3.43 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// <copyright file="AccountController.Windows.cs" auther="Mohammad Younes">
// Copyright 2013 Mohammad Younes.
//
// Released under the MIT license
// http://opensource.org/licenses/MIT
//
// </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using MixedAuth.Models;
namespace MixedAuth.Controllers
{
[Authorize]
public partial class AccountController : Controller
{
//
// POST: /Account/WindowsLogin
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<ActionResult> WindowsLogin(string userName, string returnUrl)
{
if (!Request.LogonUserIdentity.IsAuthenticated)
{
return RedirectToAction("Login");
}
var loginInfo = GetWindowsLoginInfo();
// Sign in the user with this external login provider if the user already has a login
var user = await UserManager.FindAsync(loginInfo);
if (user != null)
{
await SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
else
{
// If the user does not have an account, then prompt the user to create an account
var name = userName;
if (string.IsNullOrEmpty(name))
name = Request.LogonUserIdentity.Name.Split('\\')[1];
var appUser = new ApplicationUser() { UserName = name };
var result = await UserManager.CreateAsync(appUser);
if (result.Succeeded)
{
result = await UserManager.AddLoginAsync(appUser.Id, loginInfo);
if (result.Succeeded)
{
await SignInAsync(appUser, isPersistent: false);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
ViewBag.ReturnUrl = returnUrl;
ViewBag.LoginProvider = "Windows";
return View("WindowsLoginConfirmation", new WindowsLoginConfirmationViewModel { UserName = name });
}
}
//
// POST: /Account/WindowsLogOff
[HttpPost]
[ValidateAntiForgeryToken]
public void WindowsLogOff()
{
AuthenticationManager.SignOut();
}
//
// POST: /Account/LinkWindowsLogin
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> LinkWindowsLogin()
{
string userId = HttpContext.ReadUserId();
//didn't get here through handler
if (string.IsNullOrEmpty(userId))
return RedirectToAction("Login");
HttpContext.Items.Remove("windows.userId");
//not authenticated.
var loginInfo = GetWindowsLoginInfo();
if (loginInfo == null)
return RedirectToAction("Manage");
//add linked login
var result = await UserManager.AddLoginAsync(userId, loginInfo);
//sign the user back in.
var user = await UserManager.FindByIdAsync(userId);
if (user != null)
await SignInAsync(user, false);
if (result.Succeeded)
return RedirectToAction("Manage");
return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
}
#region helpers
private UserLoginInfo GetWindowsLoginInfo()
{
if (!Request.LogonUserIdentity.IsAuthenticated)
return null;
return new UserLoginInfo("Windows", Request.LogonUserIdentity.User.ToString());
}
#endregion
}
}