Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api 通过Xamarin表单应用程序登录_Asp.net Web Api_Xamarin_Asp.net Mvc 5_Xamarin.android - Fatal编程技术网

Asp.net web api 通过Xamarin表单应用程序登录

Asp.net web api 通过Xamarin表单应用程序登录,asp.net-web-api,xamarin,asp.net-mvc-5,xamarin.android,Asp.net Web Api,Xamarin,Asp.net Mvc 5,Xamarin.android,我们有一个带有个人用户身份验证的MVC5应用程序,还有一个xamarin表单应用程序。当我们通过正在创建的xamarin应用程序登录时,我们需要能够使用在web应用程序上创建的相同登录详细信息。我们已经成功地使用web应用程序中的现有模型创建web api控制器,并在xamarin应用程序中读取/写入数据。但问题是,我们无法为xamarin应用程序提供与我们相同的身份验证(用户名和密码,角色分配给用户)。我们如何制作从现有数据库读取的api控制器。请注意,我们的应用程序托管在azure上,带有s

我们有一个带有个人用户身份验证的MVC5应用程序,还有一个xamarin表单应用程序。当我们通过正在创建的xamarin应用程序登录时,我们需要能够使用在web应用程序上创建的相同登录详细信息。我们已经成功地使用web应用程序中的现有模型创建web api控制器,并在xamarin应用程序中读取/写入数据。但问题是,我们无法为xamarin应用程序提供与我们相同的身份验证(用户名和密码,角色分配给用户)。我们如何制作从现有数据库读取的api控制器。请注意,我们的应用程序托管在azure上,带有sql数据库


基本上,我们希望通过移动应用程序登录我们的web应用程序

你需要看一看阿德里安·霍尔的书——第2章介绍了你所需要的自定义身份验证

关键点是将移动应用程序设置为在Azure门户中使用身份验证,但不设置任何身份验证提供商(这使其成为自定义)

然后,您需要实现您自己的自定义身份验证控制器来处理身份验证回调,如Adrian书中的示例所示

using System;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Security.Claims;
using System.Web.Http;
using Microsoft.Azure.Mobile.Server.Login;
using Newtonsoft.Json;

namespace AWPBackend.Controllers
{
    [Route(".auth/login/custom")]
    public class CustomAuthController : ApiController
    {
        private MobileServiceContext db;
        private string signingKey, audience, issuer;

        public CustomAuthController()
        {
            db = new MobileServiceContext();
            signingKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY");
            var website = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");
            audience = $"https://{website}/";
            issuer = $"https://{website}/";
        }

        [HttpPost]
        public IHttpActionResult Post([FromBody] User body)
        {
            if (body == null || body.Username == null || body.Password == null ||
                body.Username.Length == 0 || body.Password.Length == 0)
            {
                return BadRequest(); ;
            }

            if (!IsValidUser(body))
            {
                return Unauthorized();
            }

            var claims = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, body.Username)
            };

            JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
                claims, signingKey, audience, issuer, TimeSpan.FromDays(30));
            return Ok(new LoginResult()
            {
                AuthenticationToken = token.RawData,
                User = new LoginResultUser { UserId = body.Username }
            });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool IsValidUser(User user)
        {
            return db.Users.Count(u => u.Username.Equals(user.Username) && u.Password.Equals(user.Password)) > 0;
        }
    }

    public class LoginResult
    {
        [JsonProperty(PropertyName = "authenticationToken")]
        public string AuthenticationToken { get; set; }

        [JsonProperty(PropertyName = "user")]
        public LoginResultUser User { get; set; }
    }

    public class LoginResultUser
    {
        [JsonProperty(PropertyName = "userId")]
        public string UserId { get; set; }
    }
实际的自定义身份验证在IsValidUser函数中进行,并应链接到现有的内部身份验证方法(此处不要使用示例,这仅用于演示)


自定义身份验证必须使用客户端流,这也符合您的要求。

通常不鼓励只包含链接的答案,因为链接可能会损坏或更改。请在答案中回答问题。是的,我理解,但因为答案是整本书的一章,所以粘贴在这里似乎毫无意义——但感谢您的帮助。我颠倒了我的投票——如果它真的是整章,问题应该标记为太宽泛,但通常情况下,可以用更少的文字解释。