I have created a web application. When I built it, I marked "Organizational accounts"
This works well - I am logged in with my Office 365 account, and it User.Identity.Name
contains an email address
This app is a front-end replacement for the old ASP Classic app. The application has an existing security table that I need to use.
I want to use the email address to search for entries in this table to get
I want to see this as soon as I go through authentication and save these two values ββuntil Session
, to refer to later
I have an existing method that does all this searching and caching. I really got it working by calling it from a view _LoginPartial.cshtml
, but it is obviously wrong to run this view from a view
Here is the code for searching and caching user information. So far it is in AccountController.cs
, but it should not be
private Boolean GetAdditionalUserInfo()
{
if (Session["UID"] != null) return true;
var r = (
from e in db.Employees
where e.Email == User.Identity.Name
select new
{
e.Emp_ID,
e.Group_ID
}
).SingleOrDefault();
if ((r == null) || (r.Group_ID == (int)Role.Inactive))
{
return false;
}
Employee ell = db.Employees.Find(r.Emp_ID);
ell.LastLogin = DateTime.Now;
db.SaveChangesAsync();
Session["UID"] = r.Emp_ID;
Session["Role"] = r.Group_ID;
return true;
}
, User.Identity.Name
, , ( , ), , - OnAuthentication
, , OnAuthenticated
. :
https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onauthentication(v=vs.118).aspx
https://msdn.microsoft.com/en-us/library/microsoft.owin.security.microsoftaccount.microsoftaccountauthenticationprovider.onauthenticated(v=vs.113).aspx
, OO - , , .
, Startup.Auth.cs
, Startup.Auth.cs
. Startup.Auth.cs
, , "" . ( , app.UseKentorOwinCookieSaver();
, , -, Session
, !!!)
- GetAdditionalUserInfo()
? ? , , , .
public partial class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
private static string authority = aadInstance + "common";
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(
CookieAuthenticationDefaults.AuthenticationType);
app.UseKentorOwinCookieSaver();
app.UseCookieAuthentication(
new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false
},
Notifications = new OpenIdConnectAuthenticationNotifications
{
RedirectToIdentityProvider = ctx =>
{
bool isAjaxRequest = (ctx.Request.Headers != null && ctx.Request.Headers["X-Requested-With"] == "XMLHttpRequest");
if (isAjaxRequest)
{
ctx.Response.Headers.Remove("Set-Cookie");
ctx.State = NotificationResultState.HandledResponse;
}
return System.Threading.Tasks.Task.FromResult(0);
}
}
});
}
}