How to use authentication cookie from WCF Authentication Service in an ASP.Net MVC application

后端 未结 2 1366
慢半拍i
慢半拍i 2021-01-30 11:57

Okay, I\'ve had little luck finding any documentation or tutorials for my specific scenario.

I have an ASP.Net MVC web application that will be using WCF services for ev

2条回答
  •  遇见更好的自我
    2021-01-30 12:39

    I have recently been trying to implement the same functionality you have described. I have managed to get it working with the following code:

        private readonly AuthenticationServiceClient service = new AuthenticationServiceClient();
    
        public void SignIn(string userName, string password, bool createPersistentCookie)
        {
            using (new OperationContextScope(service.InnerChannel))
            {
                // login
                service.Login(userName, password, String.Empty, createPersistentCookie);
    
                // Get the response header
                var responseMessageProperty = (HttpResponseMessageProperty)
                    OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
    
                string encryptedCookie = responseMessageProperty.Headers.Get("Set-Cookie");
    
                // parse header to cookie object
                var cookieJar = new CookieContainer();
                cookieJar.SetCookies(new Uri("http://localhost:1062/"), encryptedCookie);
                Cookie cookie = cookieJar.GetCookies(new Uri("http://localhost:1062/"))[0];
    
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                if (null != ticket)
                {
                    //string[] roles = RoleManager.GetRolesFromString(ticket.UserData); 
                    HttpContext.Current.User = new GenericPrincipal(new FormsIdentity(ticket), null);
                    FormsAuthentication.SetAuthCookie(HttpContext.Current.User.Identity.Name, createPersistentCookie);
                }
            }
        }
    

    It does exactly what you have described the comment to your question.

    EDIT

    I am posting here the Server-Side portion of this code for reference.

    public class HttpResponseMessageInspector : BehaviorExtensionElement, IDispatchMessageInspector, IServiceBehavior
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
    
            HttpRequestMessageProperty httpRequest = request.Properties[HttpRequestMessageProperty.Name]
            as HttpRequestMessageProperty;
    
            if (httpRequest != null)
            {
                string cookie = httpRequest.Headers[HttpRequestHeader.Cookie];
    
                if (!string.IsNullOrEmpty(cookie))
                {
                    FormsAuthentication.Decrypt(cookie);
                    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(cookie);
                    string[] roles = PrincipalHelper.GetUserRoles(authTicket);
                    var principal = new BreakpointPrincipal(new BreakpointIdentity(authTicket), roles);
    
                    HttpContext.Current.User = principal;                  
                }
                // can deny request here
            }
    
            return null;
        }
    }
    

提交回复
热议问题