Windows Authentication with ASP.NET MVC

后端 未结 3 1060
悲&欢浪女
悲&欢浪女 2020-12-24 03:58

I\'ve built a custom login system for my asp.net mvc 1.0 web application as I store large amounts of user data for each user (I decided against trying to add custom tables f

3条回答
  •  清歌不尽
    2020-12-24 04:48

    Here's how we've done it for a hybrid forms/windows authentication app:-

    public class MyBaseController
    {
      protected override void OnAuthorization( AuthorizationContext authContext )
      {
        if
        (
          !User.Identity.IsAuthenticated &&
          Request.LogonUserIdentity != null &&
          Request.LogonUserIdentity.IsAuthenticated
        )
        {
          String logonUserIdentity = Request.LogonUserIdentity.Name;
          if ( !String.IsNullOrEmpty(logonUserIdentity) )
          {
            User loginUser =
              Context.Users.FirstOrDefault(
                x => x.UserIdentity == logonUserIdentity);
            if ( loginUser != null )
              FormsAuthentication.SetAuthCookie(
                loginUser.LoginName,createPersistentCookie);
        }
      }
    

    There's some encapsulation that I've taken out for the sake of compactness.

提交回复
热议问题