Windows Authentication and SQL Membership Services

I have an ASP.Net MVC intranet site that uses Windows authentication to find out who is logged in (viewing anon is allowed). The first time users visit, I collect from them the most basic information for my Contact object (for example, name, email address, country), which is then stored in the application database.

I want to create a site role, so I need to assign a role to each user (user, administrator, etc.). I could do this with ADS groups, but that seems pretty heavyweight. Can I use the SQL membership services provided by ASP.Net to store my user names and then the roles to which they belong, or will I be forced to collect passwords, etc. (Winning the point of using Windows authentication)? Does it also integrate with the ASP.Net MVC [Authorize] attribute?

+3
source share
2 answers

"" ASP.NET, , ( Windows SQL ), MVC.

, .

+7

, .

IsInRole IPrincipal, , .

IPrincipal AuthenticateRequest Global.asax , .

, , :

private void Application_AuthenticateRequest(object sender, EventArgs e)
{
    if (!Request.IsAuthenticated)
    {
        Context.User = new MyPrincipal { Identity = new MyIdentity 
                { Type = UserType.Inactive, Id = int.MinValue }};
        Thread.CurrentPrincipal = Context.User;
    }
    else
    {
        HttpCookie authCookie = Request.Cookies[
            FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket =
                   FormsAuthentication.Decrypt(authCookie.Value);

            var identity = Db.GetIdentity(
              authTicket.Name, new HttpRequestWrapper(Request));
        Context.User = new MyPrincipal { Identity = new MyIdentity 
                { Type = UserType.Inactive, Id = int.MinValue }};
        Thread.CurrentPrincipal = Context.User;
        }
    }
}
+2

Source: https://habr.com/ru/post/1708742/


All Articles